+ 1
Please check this program,i only get 2 as output . question is to find prime numbers 1 to 100
#include<stdio.h> int main () { int i,j,c=0; for (i=2;i<=100;i++) { for(j=1;j<=i;j++) { if(i%j==0) { c=c+1; } } if (c==2) printf("%d\n",i); } return 0; }
8 Answers
+ 1
Because 2 prime. And printing after that you need set c=0 again. otherwise it is always greater than 2..
+ 2
AnonymousGuy I know đđ
+ 1
Initialize c inside the outer for loop and make j<=100. That is all I guess.
Edit: j<=100 not required.
+ 1
AnonymousGuy đ yeah my bad, I did not notice but it would still give you a right answer with just some useless number of iterations. I would change that.
+ 1
Avinesh Yeah..it will give same answers but the time required to execute it will be increased to a great extentđ
0
Avinesh
Why j<=100?? A number can never be divided by a number larger than itself..
Eg. 5 cannot never be divided by 6 so you only need to run loop until 5..
0
Thanks Jaya Krishna ,it worked
0
Raikamal Mukherjee
You're well come...