+ 1
What is wrong with my code(what should be there for negative exponents)[N.B.:you can't use power function.]
#include <iostream> int is_prime(); using namespace std; int main() { int num; cin >> num; // Reading input from STDIN for(int i=2;i*i<=num;i++){ if(is_prime(i)==1) { cout<<i<<" "; } return 0; } int is_prime(int x){ for(int i=2;i*i<=x;i++){ if(i%x==0) return 1; else return 0; } }
2 Answers
+ 2
According to your code logic, it prints whatever number which isn't prime. Also, your function prototype signature does not tally with the definition. int is_prime(int); The way the braces are placed is also wrong. The loop condition in main is wrong. The return statement in the function which checks for prime is also wrong.
+ 1
#include <iostream>
using namespace std;
int is_prime(int);
int main() {
int num;
cin >> num;
for(int i=2; i <= num; i++)
if(is_prime(i))
cout<<i<<" ";
return 0;
}
int is_prime(int x) {
for(int i=2;i*i<=x;i++)
if(x%i==0) return 0;
return 1;
}