by Dinesh Thakur Category: Control Structures

we have seen how to test whether a given number is prime or not. The program segment given below uses a for loop to test each number in a given range and print it if it is prime.

class PrimeNumbers100
{
            public static void main(String args[] )
            {
                        int i,j,x,m;
                                 System.out.println("Prime Numbers up to 100 are\n");
                        System.out.print("2"+ " ");
                        System.out.print("3"+ " ");
                         for(i=4;i<=100;i++)
                         {
                                    x=0;
                                    for(j=2;j<=i-1;j++)
                                    {
                                                m=i%j;
                                                if(m==0)
                                                {
                                                            x=1;
                                                            break;
                                                }
                                    }
                                    if(x==0)
                                    {
                                                               
                                                System.out.print(i + " ");
                                    }
                        }
            }
}

Print all the Prime Numbers up to 100



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles