Prime number in java
Prime number is that number which is divisible by 1 and by itself. Or
we can say a prime number is a positive natural number greater than 1 and have
two factors 1 and by itself. A natural number greater than 1 that is not prime
is called a composite number.
Example of Prime is 3, 5, 7, 11, 13……..
Here say 5 is only divisible by 1 and the number itself that is 5. If any the number is divisible by other than 1 and by itself is not the prime number.
Prime number first method
public class PrimeNumberFirstMetod
{
public static void main(String
args[])
{
int i, f=1, n=5;
for(i=2; i<n; i++)
{
if(n % i == 0)
{
f = 0;
break;
}
}
if(f==1)
{
System.out.println ("
Prime Number ");
}
else
{
System.out.println ("
Not a Prime Number ");
}
}
}
In the first program we have taken n=5 and check the number divisible
by other than 1 and the number itself that is 5. So here 5 is only divisible by
1 and 5 but not 2, 3, 4 so it is not a prime number.
Prime number second method
{
public static void main(String
args[])
{
int i, f=1, c=0, n=7;
for(i=1; i<=n; i++)
{
if(n % i==0)
{
c++; // c=c+1;
}
}
if(c==2)
{
System.out.println("Prime Number");
}
else
{
System.out.println("Not a Prime Number");
}
}
}
In the second program we have taken n=7 and sum the number 1 and the
number by itself, because a prime number is only divisible by 1 and that number
or have only two factor. In this case 1 and 7 and sum it in ‘c’ variable. And
the check the number by two, if it two then that number is prime number.
Prime Number with the method isPrime(int n)
public class PrimeNumberMethod
{
int flag, num, i;
public PrimeNumberMethod() //
Constructor
{
flag = 1;
num = 0;
}
int isPrime(int n)
{
num = n;
for(i=2; i<num; i++)
{
if(num % i == 0)
{
flag = 0; // change the value of flag to zero
break; // if divisble by othen 1 and the number by
itself
}
}
return flag; // return
flag 1 or 0
}
public static void
main(String args[])
{
PrimeNumberMethod ob =new
PrimeNumberMethod(); // object creation
Scanner sc = new
Scanner(System.in); // for input number
int number, fl;
System.out.println("Enter a number to chaeck prime or not");
number = sc.nextInt();
fl = ob.isPrime(number);
if(fl == 1)
{
System.out.println("Prime Number");
}
else
{
System.out.println("Not Prime number");
}
}
}
Prime number program in java wit Recursive method
import java.util.*;
class PrimeNumberWithRecursive
{
int flag; // Instance
variable
public
PrimeNumberWithRecursive()
{
flag = 1;
}
// Method to check the prime
number
int isPrime(int n, int i)
{
if (n <= 2)
{
if(n==1)
return flag=1;
else
return flag=0;
}
if (n % i == 0)
return flag=0;
if (i * i > n)
return flag=1;
// Again Check for next
divisor
return isPrime(n, i + 1);
}
// Main method to check prime
number
public static void
main(String[] args)
{
int number, fl, chk=2; //
Local variab;e
Scanner sc = new
Scanner(System.in);
PrimeNumberWithRecursive
ob = new PrimeNumberWithRecursive(); // Object creation
System.out.println("Enter a number to check prime or not");
number = sc.nextInt();
fl = ob.isPrime(number,
chk);
if(fl == 1)
System.out.println("Prime number");
else
System.out.println("Not a prime number");
}
}