Java program to display prime numbers from 1 to 100

Let’s learn java program to display prime numbers from 1 to 100.

Java program to display prime numbers from 1 to 100

Prime number program in java is most common java interview question. A prime number is one which is divisible by 1 and itself.

java program to display prime numbers from 1 to 100

Here’s the java program to display prime numbers from 1 to 100.

public class PrimeNumbersFrom1To100
{
   public static void main(String[] args)
   {
      int maxNumber = 100;
      boolean prime = true;
      String primeNumbers = "";
      for(int a = 1; a <= maxNumber; a++)
      {
         prime = checkPrime(a);
         if(prime)
         {
            primeNumbers = primeNumbers + a + " ";
         }
      }
      System.out.println("Display prime numbers from 1 to " + maxNumber + ": ");
      System.out.println(primeNumbers);
   }
   public static boolean checkPrime(int number)
   {
      int remainder;
      for(int a = 2; a <= number / 2; a++)
      {
         remainder = number % a;
         // if remainder is equal to 0 then number is not prime
         if(remainder == 0)
         {
            return false;
         }
      }
      return true;
   }
}


Output:

Display prime numbers from 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


Similarly we can display prime numbers from 1 to n. Here’s the java program to display prime numbers from 1 to n.

import java.util.Scanner;
public class PrimeNumber
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int a = 0;
      int number = 0;
      String strPrimeNumber = "";
      System.out.println("Please enter the value of n : ");
      int n = sc.nextInt();
      for(a = 1; a <= n; a++)
      {
         int count = 0;
         for(number = a; number >= 1; number--)
         {
            if(a % number == 0)
            {
               count = count + 1;
            }
         }
         if(count == 2)
         {
            strPrimeNumber = strPrimeNumber + a + " ";
         }
      }
      System.out.println("Java program to display prime numbers from 1 to n : ");
      System.out.println(strPrimeNumber);
      sc.close();
   }
}


Output:

Please enter the value of n : 200
Java program to display prime numbers from 1 to n :
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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199


Also read – java program to print prime numbers in a given range

Also read – switch statement in java

Also read – print first 10 prime numbers in java

Also read – prime number program in java using scanner

Also read – java program to print prime numbers from 2 to n


prime number program in java using for loop

Let’s learn prime number program in java using for loop.

import java.util.Scanner;
public class PrimeNumberUsingForLoop
{
   public static void main(String[] args)
   {
      int counter = 0;
      System.out.println("Please enter a number: ");
      Scanner sc = new Scanner(System.in);
      int number = sc.nextInt();
      for(int a = 1; a <= number; a++)
      {
         if(number % a == 0)
         {
            counter++;
         }
      }
      if(counter == 2)
      {
         System.out.println(number + " is prime number.");
      }
      else
      {
         System.out.println(number + " is not a prime number.");
      }
      sc.close();
   }
}


Output:

Please enter a number: 66
66 is not a prime number.

Please enter a number: 17
17 is prime number.


Java program to print prime numbers from 1 to 100 using while loop

Here’s the java program to print prime numbers from 1 to 100 using while loop.

public class PrimeNumberUsingWhileLoop
{
   public static void main(String[] args) 
   {
      int a, number = 1, counter;
      System.out.println("Print prime numbers from 1 to 100 using while loop: "); 
      while(number <= 100)
      {
         counter = 0;
         a = 2;
         while(a <= number/2)
         {
            if(number % a == 0)
            {
               counter++;
               break;
            }
            a++;
         }
         if(counter == 0 && number != 1)
         {
            System.out.print(number + " ");
         }
         number++;
      }
   }
}


Output:

Print prime numbers from 1 to 100 using while loop:
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