Thursday, January 30, 2014

To find given number is Prime or Not

Write a program to find given number is Prime or Not


import java.util.Scanner;
public class PrimeOrNot

{

    public static void main(String[] args)

  {
		 System.out.println("Enter the number:");
		 Scanner sc=new Scanner(System.in);
		 int i;
		 int flag=0;
		 long num=sc.nextLong();

		 if(num<=3)

			 System.out.println("The given number is prime:");

		 else
		 
		 for(i=2;i<=num/2;i++)

			  if(num%i==0)

		  {
		  flag=1;
		  break;

		  }

		else

		  flag=0;

		  if(flag==1)
				System.out.println("The given number "+num+"  is not prime");
		  else
				System.out.println("The given number "+num+" is prime");
			}
}



OUTPUT 1:


Enter the number:
5

The given number 5 is prime



OUTPUT 2:

Enter the number:
55


The given number 55  is not prime





No comments:

Post a Comment