Sunday 25 September 2011

Program to check whether given number is prime or not..?(java)


Prime Number
A prime number is a number divisible only by itself and 1. Aprime number is a positve whole number that has exactly two distinct positive divisers.
A prime number is a whole number p that is strictly greater that 1, and such that if p divides a product a*b then p divides at least one of a and b. 


Program to check whether given number is prime or not..?


import java.util.*;  // importing package util


public class Prime
{
public static void main (String args[])
{
int num,count=0;
Scanner scan = new Scanner(System.in); //scanner for input
System.out.print("Enter any number : ");
num = scan.nextInt();
for(int i=2; i <= (num/2); i++) 
{
if((num % i) == 0)
{
count++;
break;
}
}
if((count==0) && (num!= 1))
System.out.println( num + " is a prime number.");
else
System.out.println( num + " is not a prime number.");
}
}

No comments:

Post a Comment