Table of Contents
Program to display prime numbers from 1 to 100 or 1 to n in Java
In this article, we will discuss the concept of Program to display prime numbers from 1 to 100 or 1 to n in Java
In this program, we are going to learn how to write the code to display prime numbers 1 to n using several ways in Java language.
This is done using for loop , while loop , do-while loop and method in Java language
Print prime numbers from 1 to 100 or 1 to n in Java
Print prime numbers from 1 to 100 or 1 to n using for loop
In this program, we will print prime numbers from 1 to 100 or 1 to n using a for loop in Java language
Program 1
import java.util.Scanner;
public class DisplayPrime{
public static void main(String args[]){
int i,count;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are : ");
for(int j=2; j<=n; j++){
count=0;
for(i=1; i<=j; i++){
if(j%i==0){
count++;
}
}
if(count==2)
System.out.print(j+" ");
}
}
}
When the above code is executed, it produces the following result
Case 1
Please Enter value for n: 100 Prime numbers between 1 to 100 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Case 2
Please Enter value for n: 25 Prime numbers between 1 to 25 are : 2 3 5 7 11 13 17 19
Print prime numbers from 1 to 100 or 1 to n using while loop
In this program, we will print prime numbers from 1 to 100 or 1 to n using a while loop in Java language
Program 2
import java.util.Scanner;
public class DisplayPrimeWhile{
public static void main(String args[]){
int i,count;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are : ");
int j=2;
while(j<=n){
count=0;
i=1;
while(i<=j){
if(j%i==0){
count++;
}
i++;
}
if(count==2)
System.out.print(j+" ");
j++;
}
}
}
When the above code is executed, it produces the following result
Case 1
Please Enter value for n: 100 Prime numbers between 1 to 100 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Case 2
Please Enter value for n: 50 Prime numbers between 1 to 50 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Print prime numbers from 1 to 100 or 1 to n using do-while loop
In this program, we will print prime numbers from 1 to 100 or 1 to n using a do-while loop in Java language
Program 3
import java.util.Scanner;
public class DisplayPrimeDoWhile{
public static void main(String args[]){
int i,count;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are : ");
int j=2;
do{
count=0;
i=1;
do{
if(j%i==0){
count++;
}
i++;
}while(i<=j);
if(count==2)
System.out.print(j+" ");
j++;
}while(j<=n);
}
}
When the above code is executed, it produces the following result
Case 1
Please Enter value for n: 100 Prime numbers between 1 to 100 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Case 2
Please Enter value for n: 70 Prime numbers between 1 to 70 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
Print prime numbers from 1 to 100 or 1 to n using method
In this program, we will print prime numbers from 1 to 100 or 1 to n using a method in Java language
Program 4
import java.util.Scanner;
public class DisplayPrimemethod{
public static void main(String args[]){
int i,count;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are : ");
for(i=2; i<=n; i++){
if(printPrime(i)){
System.out.print(i+" ");
}
}
}
public static boolean printPrime(int n)
{
boolean flag =true;
if(n<=1)
flag =false;
for(int i=2; i<=n/2; i++){
if(n%i==0){
flag =false;
break;
}
}
return flag;
}
}
When the above code is executed, it produces the following result
Case 1
Please Enter value for n: 100 Prime numbers between 1 to 100 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Case 2
Please Enter value for n: 60 Prime numbers between 1 to 60 are : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
Suggested for you
if statements in Java language
Data type and variable in Java
Do-while loop in Java language
Similar post
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
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