+ 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; }

27th Nov 2019, 12:40 PM
Raikamal Mukherjee
Raikamal Mukherjee - avatar
8 Réponses
+ 1
Because 2 prime. And printing after that you need set c=0 again. otherwise it is always greater than 2..
27th Nov 2019, 1:02 PM
Jayakrishna 🇮🇳
+ 2
AnonymousGuy I know 😂😅
27th Nov 2019, 2:11 PM
Avinesh
Avinesh - avatar
+ 1
Initialize c inside the outer for loop and make j<=100. That is all I guess. Edit: j<=100 not required.
27th Nov 2019, 1:05 PM
Avinesh
Avinesh - avatar
+ 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.
27th Nov 2019, 2:07 PM
Avinesh
Avinesh - avatar
+ 1
Avinesh Yeah..it will give same answers but the time required to execute it will be increased to a great extent😂
27th Nov 2019, 2:10 PM
Alaska
Alaska - avatar
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..
27th Nov 2019, 1:52 PM
Alaska
Alaska - avatar
0
Thanks Jaya Krishna ,it worked
27th Nov 2019, 2:39 PM
Raikamal Mukherjee
Raikamal Mukherjee - avatar
0
Raikamal Mukherjee You're well come...
27th Nov 2019, 3:20 PM
Jayakrishna 🇮🇳