Java Program to find Prime Number, Write a program in Java Language to find a twin prime numbers.
Program
class prime
{
public static void main(String args[])
{
Logic L=new Logic(); // Create instance of Logic class
L.findprime();
}
}
class Logic
{
public void findprime()
{
int i,j,flag;
flag=0;
System.out.println("Twin Prime Numbers are.......");
for(i=1;i<100;i=i+2) // Logic to find First prime number
{
flag=0;
for(j=2;j<i-1;j++)
{
if( i % j == 0 )
{
flag = 1;
break;
}
}
if (flag == 0) // to find number is twin prime or not
{
for(j=2;j<(i+2)-1;j++)
{
if((i+2) % j == 0 )
{
flag = 1;
break;
}
}
if (flag == 0)
{
System.out.println(i + " " + (i+2));
}
}
}
}
}
No comments:
Post a Comment