java

Java program for display first n prime numbers

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

Nested if in Java language

Data type and variable in Java

Operator in Java language

Method in Java language

For loop in Java language

while loop in Java language

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

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.