In this tutorial, we will discuss the concept of an Example program – write a program to find first n prime numbers in Java.
In this post, we are going to learn how to write a program to find the first n prime number using for, while, and do-while loops in Java programming language:
The number that can be divided by 1 and itself only. it is called the prime number
for Example 2,3,5,7,11,13,….
To understand this example program, you should have previous knowledge of the following Java topics
for loop in Java language
while loop in Java language
do-while loop in Java language
In this program, we will print the first “n” prime numbers, using for loop. first, the user is asked to enter a number randomly for n, and then the program prints the prime numbers 1 to n.
Program 1
//print the prime number in given range(0 - n) import java.util.*; public class PrimeNum_firstN1{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //scanner class for input int num=1,y=3,count,j; //variable declaration and initialization System.out.println("Please Enter the number for range(0 - n): "); int n=sc.nextInt(); if(n>=1){ System.out.println("The first "+n+" prime numbers are: " ); System.out.println(2); } for(count=2; count<=n;){ for(j=2; j<=Math.sqrt(y); j++){ if(y%j==0){ num=0; break; } } if(num!=0){ System.out.println(y); count++; } num=1; y++; } } }
When the above code is executed, it produces the following result
Please Enter the number for range(0 - n): 10 The first 10 prime numbers are: 2 3 5 7 11 13 17 19 23 29
In this program, we will print the first “n” prime numbers, using a while loop. first, the user is asked to enter a number randomly for n, and then the program prints the prime numbers 1 to n.
Program 2
//print first n prime numbers using while import java.util.*; public class Prime_firstNwhile{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //scanner class for input int n,num=1,y=3,count,j; System.out.println("Please Enter the number for print prime: "); n=sc.nextInt(); if(n>=1){ System.out.println("First "+n+" prime numbers are: " ); System.out.println(2); } count=2; while(count<=n){ j=2; while(j<=Math.sqrt(y)){ if(y%j==0){ num=0; break; } j++; } if(num!=0){ System.out.println(y); count++; } num=1; y++; } } }
When the above code is executed, it produces the following result
Please Enter the number for range(0 - n): 5 The first 5 prime numbers are: 2 3 5 7 11
In this program, we will print the first “n” prime numbers, using a do-while loop. first, the user is asked to enter a number randomly for n, and then the program prints the prime numbers 1 to n.
Program 2
//Print first n prime numbers using do-while import java.util.*; public class Prime_FirstNDowhile2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //scanner class for input int n,num=1,y=3,count,j; System.out.println("Please Enter the number for print prime: "); n=sc.nextInt(); if(n>=1){ System.out.println("The First "+n+" prime numbers are: " ); //display first n priume number System.out.println(2); } count=2; do{//outer do-while j=2; do{//inner do-while if(y%j==0){ num=0; break; } j++; }while(j<=Math.sqrt(y)); if(num!=0){ System.out.println(y); count++; } num=1; y++; }while(count<=n); } }
When the above code is executed, it produces the following result
Please Enter the number for print prime: 7 The first 5 prime numbers are: 2 3 5 7 11 13 17
Suggested for you
Data type and variable in Java language
The operator in Java language
Similar post
Program to find whether a Number is Prime or Not in C++
Program to find whether a Number is Prime or Not in C
Program to find whether a Number is Prime or Not in Java
Program to find whether a Number is Prime or Not in Python
C++ program to find first n prime numbers
Java program to find first n prime numbers
C program to find first n prime numbers
C code to 5 ways to check whether the given integer is Even or Odd
Java code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
Python code to 5 ways to check whether the given integer is Even or Odd
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…