Program to Find Prime Number – Java

- Advertisement -

 

- Advertisement -

package JavaPrograms;
import java.util.*;

public class PrimeNumber {
	//Note: Scanner class work with JDK1.5 or above

		public static void main(String args[])
		{		
			int n, i, res;
			boolean flag=true;

			System.out.println("Please Enter a No.");
			Scanner scan= new Scanner(System.in);
			n=scan.nextInt();
			for(i=2;i<=n/2;i++)
			{
				res=n%i;
				if(res==0)
				{
					flag=false;
					break;
				}
			}
			if(flag)
				System.out.println(n + " is Prime Number");
			else
				System.out.println(n + " is not Prime Number");
		}
	}

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More