How to find the given number is prime in java
class Testprime
public static void main(String args[])
{
System.out.println("Enter the any number..");
String number;
int f=0;
int n=Integer.parseInt(number);
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
f=f+1;
}
}
if(f==2)
System.out.println("The Given number is prime "+n);
else
System.out.println("The Given number is Not prime "+n);
}
}
output:
Enter the any number 5
The Given number is prime 5
because the prime has only two factors is called prime number. i.e the prime number divide only 1 and itself is called the prime number
so 5 is the prime number because 5 is not divide any number .
Again compile
Enter the any number 6
The Given number is not prime 6
so 6 is not prime because 6 is divide by the 2 and 3.
class Testprime
public static void main(String args[])
{
System.out.println("Enter the any number..");
String number;
int f=0;
int n=Integer.parseInt(number);
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
f=f+1;
}
}
if(f==2)
System.out.println("The Given number is prime "+n);
else
System.out.println("The Given number is Not prime "+n);
}
}
output:
Enter the any number 5
The Given number is prime 5
because the prime has only two factors is called prime number. i.e the prime number divide only 1 and itself is called the prime number
so 5 is the prime number because 5 is not divide any number .
Again compile
Enter the any number 6
The Given number is not prime 6
so 6 is not prime because 6 is divide by the 2 and 3.
No comments:
Post a Comment