calculating all prime numbers till given number...(Akamai technical coding test question-solution )
#include <iostream>
#include <vector>
using namespace std;
bool check(int j){
for(int k = 2; k <= j/2; k++){
if(j % k == 0){
return false;
}
else {
return true;
}
}
return true;
}
void prime_num(int num){
int count = 0;
for(int j = 2; j < num; j++){
if(check(j) == true){
cout << j <<" ";
count++;
if(count == 5){
count = 0;
cout << endl;
}
}
}
}
int main(){
int n;
cin >>n;
if(n < 2){cout << "no prime nos"; }
else{
prime_num(n);
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
bool check(int j){
for(int k = 2; k <= j/2; k++){
if(j % k == 0){
return false;
}
else {
return true;
}
}
return true;
}
void prime_num(int num){
int count = 0;
for(int j = 2; j < num; j++){
if(check(j) == true){
cout << j <<" ";
count++;
if(count == 5){
count = 0;
cout << endl;
}
}
}
}
int main(){
int n;
cin >>n;
if(n < 2){cout << "no prime nos"; }
else{
prime_num(n);
}
return 0;
}
No comments:
Post a Comment