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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -