c - Why the nested 'for' loop is not working in the below program? -
#include<stdio.h> #include<conio.h> void main() { int m,a,i,b; printf("enter number upto prime number displayed:"); scanf("%d",&m); for(a=1;a<=m;a++) { for(i=1;i<=a;i++) { if(a%i==0) { b++; } } if(b==2) { printf("\t%d",a); } } getch(); }
initialize b
@ beginning of code, , inside loop:
#include<stdio.h> #include<conio.h> void main() { int m,a,i,b=0; // initialize b printf("enter number upto prime number displayed:"); scanf("%d",&m); for(a=1;a<=m;a++) { for(i=1;i<=a;i++) { if(a%i==0) { b++; } } if(b==2) { printf("\t%d",a); } b=0; // re-initialize } getch(); }
Comments
Post a Comment