Flag This Hub

How to Display Prime Numbers from 1-100 using JAVA Programming.

By


See all 13 photos

Java Prime Numbers

How to Display Prime Numbers from 1-100 using JAVA Programming.


1.) Click start.


2.) Open JCreator IDE

3. Create a new file.

4.) Select file type as Java Class.

5.) Just specify the name of the class as primes and click finish.

6.) Create a method that will perform the operation then just call it in the main method.

  7.) In the getPro method declare the following variables.

8.) Make the first loop.

9.) Declare ctr variable and initialize it to 0. This will serve as our controller to determine if the current number is prime or not.

10.) Make now the second loop inside the first loop.

11.) Inside the loop insert this command.

12.) Outside of the second loop put this condition.

13.) Finally, outside of the nested for loop display the output.

14.) And you’ll produce an output that would look like this.

Source Code:

public class primes
{
    public static void main(String[]args)
       {
            getPro();
        }
private static void getPro()
{
     String primeNumbers="";
    int x,y;
    for(x=1;x<=100;x++)
    {
        int ctr=0;
        for(y=x;y>=1;y--)
        {
            if(x%y==0)
            {
                ctr+=1;
            }
        }
        if(ctr==2)
        {
            primeNumbers+=x+" ";
        }
    }

    System.out.print("The Prime Numbers from 1-100 are : \n"+primeNumbers);
    }
}


Have fun guys!

:-)

Like this Hub?
Please wait working