Java Program to Print First n Prime Number

In this tutorial, we are going to learn writing java program to print all the prime numbers that are smaller than or equal to the number given as an input by the user.

Problem Statement

For any number that is input by the user, we have to print all the prime numbers. For example

Case1: If the user inputs number 12
then the output should be ‘2, 3, 5, 7, 11’.
Case2: If the user inputs a number 51.
then the output should be ‘2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 ’.

What is Prime Number?

Prime Numbers are the numbers that have only 2 factors 1 and the number itself. It is only defined for the number which is greater than ‘1’. ‘2’ is the smallest prime number.

Some examples

  • 5 is a prime number because only factors of 5 are ‘1 and 5’.
  • 11 is a prime number because only factors of 11 are ‘1 and 11’.
  • 10 is not prime because the factors of 10 are ‘ 1,2,5 and 10’.

Java Program to Print First n Prime Number

import java.util.*;
public class Main
public static void main(String[] args) {
    int i=0,n,temp,temp1=1;
    Scanner sc = new Scanner(;
    System.out.println("Give input to find Prime number before ");
    n= sc.nextInt();
    System.out.println("Prime numbers are before "+n);


Give input to find Prime number before 
Prime numbers are before 5


The input number is 9, so our program will check all the numbers smaller than 9 and greater than 0. The numbers that do not have any other factor other than 1 and itself, i.e. prime numbers which are smaller than 9 are 1,2,3,5, and 7.

Program Explanation

  1. Imports and Setup:
    • import java.util.*; imports the Java utility package, which includes the Scanner class for reading input from the user.
  2. Main Class and Method:
    • The program defines a public class named Main and the main method, which is the entry point of any Java application.
  3. Variable Initialization:
    • int i = 0, n, temp, temp1 = 1; declares and initializes several integer variables:
      • i is used for looping.
      • n will store the user input specifying the upper limit for finding prime numbers.
      • temp is used as a flag to indicate whether a number is prime.
      • temp1 is initialized to 1 and is used to check each number up to n.
  4. Input from User:
    • A Scanner object sc is created to read input from the console.
    • The program prompts the user to enter the value of n, which is the limit up to which prime numbers will be found.
  5. Prime Number Detection:
    • A while loop runs from 1 up to n (inclusive) using temp1 as the loop variable.
    • For each value of temp1, an inner for loop runs from 2 to temp1/2. If temp1 is divisible by any number in this range (i), temp is set to 1, and the loop breaks.
      • The division by 2 in the loop condition (temp1/2) is an optimization to reduce the number of checks required, as a number cannot have factors larger than its half (excluding itself).
  6. Prime Check and Output:
    • After the inner loop, if temp remains 0 (indicating no divisors were found other than 1 and itself), temp1 is identified as a prime number and printed.
    • temp1 is then incremented to check the next number.
  7. Loop Continuation:
    • The process continues until all numbers from 1 to n have been checked.