Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program for display first n prime numbers

Java program for display first n prime numbers

Posted on December 17, 2020December 17, 2020

Table of Contents

  • Java program to display first n prime numbers
    • Code to display first n prime numbers
      • Code to display first n prime numbers using for loop
      • Code to display first n prime numbers using while loop
      • Code to display first n prime numbers using do-while loop
      • Code to display first n prime numbers using method

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes