In this tutorial, we are going to write a Java program to print N prime numbers in java Programming with practical program code and step-by-step full complete explanation.

Java program to print N prime numbers


Java program to print N prime numbers

 
import java.util.Scanner;

public class JavaApplication2{
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
         
        // Enter number
        System.out.print("Enter number = ");
        int number = scanner.nextInt();
 
        System.out.println("Prime number are : ");
        for(int i = 2; i <= number; i++){
            int count = 0;
            for(int j = 1;j <= i;j++){
                if(i % j == 0)
                    count++;
            }
            if(count == 2)
                System.out.print(i +" ");
        }    
    }
}


Output

 
Enter number = 10
Prime numbers are : 
2 3 5 7