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
Code to display prime numbers from 1 to 100 or 1 to n in Python

Program to display prime numbers from 1 to 100 or 1 to n in Java

Posted on November 13, 2020November 14, 2020

Table of Contents

  • Program to display prime numbers from 1 to 100 or 1 to n in Java
    • 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
      • Print prime numbers from 1 to 100 or 1 to n using while loop
      • Print prime numbers from 1 to 100 or 1 to n using do-while loop
      • Print prime numbers from 1 to 100 or 1 to n using method
    • Related

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

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

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

 

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