PROGRAM 12 : ENTER THE LOWER LIMIT AND UPPER LIMIT TO GET PAIRS OF TWIN PRIME NUMBERS.
Example:
Example:
PROGRAM:
import java.util.Scanner;
public class twinprime
{
public static boolean isprime(int n) // CHECKS THAT THE NUMBER IS PRIME OR NOT
{ int count = 0;
for(int i = 1 ; i<=n ; i++)
{
if(n%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
public static void main(String args[])
{
twinprime obj = new twinprime();
Scanner sc = new Scanner(System.in);
System.out.println("Enter lower range:");
int a = sc.nextInt();
System.out.println("Enter upper range:");
int b = sc.nextInt();
if(a>b || a==b) // THE LOWER LIMIT CANNOT BE GREATER THAN UPPER LIMIT
System.out.println("INVALID RANGE ENTERED.");
else
{
System.out.println("The pairs of twin prime numbers in the following range are:");
for(int i = a ; i<=b-2 ; i++)
{
if(obj.isprime(i)==true && obj.isprime(i+2)==true) // CONSECUTIVE NUMBERS CAN BE PRIME
System.out.println("(" + " " + i + " , " + (i+2) + " )");
}
}
}
}
public class twinprime
{
public static boolean isprime(int n) // CHECKS THAT THE NUMBER IS PRIME OR NOT
{ int count = 0;
for(int i = 1 ; i<=n ; i++)
{
if(n%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
public static void main(String args[])
{
twinprime obj = new twinprime();
Scanner sc = new Scanner(System.in);
System.out.println("Enter lower range:");
int a = sc.nextInt();
System.out.println("Enter upper range:");
int b = sc.nextInt();
if(a>b || a==b) // THE LOWER LIMIT CANNOT BE GREATER THAN UPPER LIMIT
System.out.println("INVALID RANGE ENTERED.");
else
{
System.out.println("The pairs of twin prime numbers in the following range are:");
for(int i = a ; i<=b-2 ; i++)
{
if(obj.isprime(i)==true && obj.isprime(i+2)==true) // CONSECUTIVE NUMBERS CAN BE PRIME
System.out.println("(" + " " + i + " , " + (i+2) + " )");
}
}
}
}
No comments:
Post a Comment