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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes