Computer Applications
Write a program to input two numbers and check whether they are twin prime numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.
For example: (5,7), (11,13), ……. and so on.
Java
Java Nested for Loops
83 Likes
Answer
import java.util.Scanner;
public class KboatTwinPrime
{
public void twinPrimeCheck() {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
boolean isAPrime = true;
for (int i = 2; i <= a / 2; i++) {
if (a % i == 0) {
isAPrime = false;
break;
}
}
if (isAPrime && Math.abs(a - b) == 2) {
boolean isBPrime = true;
for (int i = 2; i <= b / 2; i++) {
if (b % i == 0) {
isBPrime = false;
break;
}
}
if (isBPrime)
System.out.println(a + " and " + b + " are twin prime");
else
System.out.println(a + " and " + b + " are not twin prime");
}
else
System.out.println(a + " and " + b + " are not twin prime");
}
}
Variable Description Table
Program Explanation
Output
Answered By
21 Likes
Related Questions
Write a program to display all the numbers between 100 and 200 which don't contain zeros at any position.
For example: 111, 112, 113, ……. , 199Write a program in Java to find the sum of the following series:
S = (a/2!) - (a/3!) + (a/4!) - (a/5!) + ……. + (a/10!)
Write a program in Java to find the sum of the following series:
S = (2/a) + (3/a2) + (5/a3) + (7/a4) + ……. to n
Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
For example: 11, 101, 131, 151,