Java – Program to Find Prime Number

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");
		}
	}

 

You might also like More from author

Loading...