Written by Sai Sameera Thota on May 31, 2013 in Java > Programming

Java Program to Print Prime Numbers in the given Range

GET ALERTS:

Get our Latest updates delivered to your mailbox!

The program takes the range as an input and displays all the prime numbers in the given range.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class prime_num 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("\nEnter the range : ");
		int range=Integer.parseInt(br.readLine());
		System.out.println();
		for(int i=2;i<=range;i++)
		{
			int flag=0;
			for(int j=2;j<i;j++)
			{
				if(i%j==0)
				{
					flag=1;
				}
			}
			if(flag==0)
				System.out.print(i+" ");
		}
		System.out.println();

	}
}

Output:
Java program to display the prime numbers

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: