Java Program to Display Prime Numbers From 1 to N

In this post, we will learn to code the Java Program to Display Prime Numbers From 1 to N. Let’s understand Prime Numbers and How to Check Prime Numbers in Java Programming Language.

In a previous post, we will How to check whether a number is prime or not. Today, we will display all the prime numbers from 1 to N.

Now, let’s see the Java Program to Display Prime Numbers From 1 to N using For Loop.

Java Program to Display Prime Numbers From 1 to N

import java.util.*;

public class Main{
    
    public static boolean isPrime(int number){
        if(number == 1) return false;
        if (number == 2) {
            return true;
        }
        else {
          int count = 0;
            //logic
          for (int div = 2; div * div <= number ; div++) {
            if (number % div == 0) {
              count++;
            }
          }
          if(count > 0){
            return false;
          }
        }
        
        return true;
    }
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the value of N :");
        int N = sc.nextInt();
        
        System.out.println("All Prime Numbers from 1 to "+N+" are :");
        for(int num = 1 ; num <= N ; num++){
            if(isPrime(num) == true){
                System.out.println(num);
            }
        }
    }
    
}

Output 1

Enter the value of N : 50

All Prime Numbers from 1 to 50 are :
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

Output 2

Enter the value of N : 11

All Prime Numbers from 1 to 11 are :
2
3
5
7
11

How Does This Program Works?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the value of N :");
        int N = sc.nextInt();

In this program,we take the input of N from the user using the Scanner Class in Java. Then, we have created a for loop from 1 to N and checked if each number is prime or not and printed each prime number.

        System.out.println("All Prime Numbers from 1 to "+N+" are :");
        for(int num = 1 ; num <= N ; num++){
            if(isPrime(num) == true){
                System.out.println(num);
            }
        }

To understand the optimized way to check prime numbers in Java Programming Language check this, Java Program to Check Prime Number.

This is the Java Program to Print Prime Numbers From 1 to N using for loop.

Java Program to Print Prime Numbers From 1 to N using While Loop

import java.util.*;

public class Main{
    
    public static boolean isPrime(int number){
        if(number == 1) return false;
        if (number == 2) {
            return true;
        }
        else {
          int count = 0;
            //logic
          for (int div = 2; div * div <= number ; div++) {
            if (number % div == 0) {
              count++;
            }
          }
          if(count > 0){
            return false;
          }
        }
        
        return true;
    }
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the value of N :");
        int N = sc.nextInt();
        
        System.out.println("All Prime Numbers from 1 to "+N+" are :");
        int num=1;
        while(num <= N){
            if(isPrime(num) == true){
                System.out.println(num);
            }
            num++;
        }
    }
    
}

Output 1

Enter the value of N : 50

All Prime Numbers from 1 to 50 are :
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

Output 2

Enter the value of N : 11

All Prime Numbers from 1 to 11 are :
2
3
5
7
11

Conclusion

I hope after going through this post, you understand Java Program to Print Prime Numbers From 1 to N and learned to code a Java Program to Check Prime Number.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *