Java Program to Print Prime Numbers

« Previous Program Next Program »

Print Prime Numbers

To print all the prime numbers between the particular range provided by the user in Java Programming, you have to check the division from 2 to one less than that number (say n-1), if the number divided to any number from 2 to on less than that number then that number will not be prime, otherwise that number will be prime number.

Java Programming Code to Print Prime Numbers

To print prime number between the given range. For example, to print prime numbers between the range 3 to 29, here 3 is the starting number and 29 is the ending number, so the following Java Program ask to the user to enter the starting and ending number to find and print all the prime numbers between the given range (here we are including the starting and ending number also if they are prime):

/* Java Program Example - Print Prime Numbers */
import java.util.Scanner;

public class JavaProgram
    public static void main(String args[])
        int start, end, i, j, count=0;
        /* to print all the prime numbers between any range
         * enter the two number (starting and ending number)
        Scanner scan = new Scanner(;
        System.out.print("Enter the Range :\n");
        System.out.print("Enter Starting Number : ");
        start = scan.nextInt();
        System.out.print("Enter Ending Number : ");
        end = scan.nextInt();
        System.out.print("Prime Numbers Between " + start + " and " +end+ " is :\n");
        for(i=start; i<=end; i++)
            count = 0;
            for(j=2; j<i; j++)
                if(i%j == 0)
            if(count == 0)
                System.out.print(i + " ");

When the above Java Program is compile and executed, it will produce the following output:

Java Program print prime numbers

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:

Java Online Test

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.