Nested Loop in Java (List of Prime Numbers from 1 to N)




 class nestedLoopDemo
{
public static void main(String args[])
{
int n, flag, i, j;
n=100;
j=2;
System.out.println("The List of Primes nos from 1 to "+n+" is:");
while(j<=n)
{
flag=0;
for(i=2;i<j;i++)
{
if(j%i==0)
flag++;
}
if(flag==0)
System.out.println(j);
j++;
}
System.out.println("End of Program.");
}
}





1 comment:

  1. Great article and simple explanation. This is really helpful for anyone trying to understand the topic better. For more structured learning, check AI powered Core JAVA Online Training in ameerpet.

    ReplyDelete

Post your comments

Powered by Blogger.