[Solved] Why the nested ‘for’ loop is not working in the below program?


initialize b at the beginning of your code, and inside the loop:

#include<stdio.h>

#include<conio.h>

void main() 
{
    int m,a,i,b=0; // initialize b

    printf("Enter the number upto which the prime number is to be 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();
}

5

solved Why the nested ‘for’ loop is not working in the below program?