Table of Contents
Java program to display first n prime numbers
In this article, we will discuss the concept of Java program for display first n prime numbers
In this code, we are going to learn how to display first n prime numbers using different methods in Java language.
This is done using for loop,while loop, do-while loop and method in Java language
Code to display first n prime numbers
Code to display first n prime numbers using for loop
In this program, we will display first n prime numbers using for loop in Java language
Program 1
import java.util.*; public class PrimePrint_firstNfor{ 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 of prime you want: "); n=sc.nextInt(); if(n>=1){ System.out.println("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 of prime you want: 18 First 18 prime numbers are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
Code to display first n prime numbers using while loop
In this program, we will display first n prime numbers using While loop in Java language
Program 2
import java.util.*; public class PrimePrint_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 of prime you want: "); 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 of prime you want: 12 First 12 prime numbers are 2 3 5 7 11 13 17 19 23 29 31 37
Code to display first n prime numbers using do-while loop
In this program, we will display first n prime numbers using do-while loop in Java language
Program 3
import java.util.*; public class PrimePrint_firstNDowhile1{ 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 of prime you want: "); n=sc.nextInt(); if(n>=1){ System.out.println("First "+n+" prime numbers are: " ); System.out.println(2); } count=2; do{ j=2; do{ 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 of prime you want: 15 First 15 prime numbers are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Code to display first n prime numbers using method
In this program, we will display first n prime numbers using method in Java language
Program 4
import java.util.*; public class PrimePrint_firstNMethod{ static void printPrimenum(int N){ int num=2,primeCount=0; while(primeCount<N){ if(isPrime(num)){ System.out.println(num+" "); primeCount++; } num++; } } static boolean isPrime(int n){ for(int j=2; j<=Math.sqrt(n); j++){ if(n%j==0) return false; } return true; } public static void main(String args[]){ Scanner sc=new Scanner(System.in); //scanner class for input System.out.println("Please Enter the number of prime you want: "); int N=sc.nextInt(); System.out.println("First "+N+" prime numbers are:"); printPrimenum(N); System.out.println(); } }
When the above code is executed, it produces the following result
Please Enter the number of prime you want: 20 First 20 prime numbers are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
Suggested for you
if statements in Java language
Data type and variable in Java
Do-while loop in Java language
Similar post
Java programming code to check prime or not
C programming code to check prime or not
C++ programming code to check prime or not
Python programming code to check prime or not
Code to print prime numbers from 1 to 100 or 1 to n in Java
Code to print prime numbers from 1 to 100 or 1 to n in C
Code to print prime numbers from 1 to 100 or 1 to n in C++
Code to print prime numbers from 1 to 100 or 1 to n in Python