Java Program to Find Prime Number between 1 to 100

Finding Prime number between 1 to 100

A prime number is a positive integer having exactly two factors. If 11 is a prime, then it’s only factors are necessarily 1 and 11 itself.  The smallest prime number as defined by modern mathematicians is 2. To be prime, a number must be divisible only by 1 and the number itself which is fulfilled by the number 2.

But, number 1 has one and only one factor which is 1 itself. So, 1 is not considered as a Prime number.

Java Program to Find Prime Number between 1 to 100

Implementation of finding Prime Number:

  • In the java program we will find prime number between 1 to 100 so firstly we create a Main class,and initialize i ,j and count=0.
  • It will print the prime number between 1 to 100 because message will be show in output .
  • The loop is start i=2 to less then equal 100 and second loop start j=1 to less then or equal i.
  • The condition is i%j=0
  • And the value of count is increment after the iteration of the loop.
  • And then count value equal of two then print the i. because prime number has only two factor one is 1 and another by the number.

Java Code to Find the Prime Number between 1 to 100:

//Java Program to find prime no 1 to 100
public class Main
{
     public static void main(String[] args)
{
    int i,j,count=0;

//print prime no between 1 to 100
System.out.println("prime number between 1 to 100\n");

//loop for printing prime no between 1 to 100
for(i=2;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)

System.out.print(" "+i);
count=0;
}
}
Output:
prime number between 1 to 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97