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.");
}
}





No comments

Post your comments

Powered by Blogger.