java program with scanner to input two no.s and find whether they are twin prime or not
Answers
Program:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
boolean flag = false;
int num1,num2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 number");
num1 = sc.nextInt();
num2 = sc.nextInt();
for(int i = 2; i <= num1/2 && i <= num2/2; ++i){
if(num1 % i == 0 && num2 % i == 0){
flag = true;
break;
}
}
if (!flag && num1==num2)
System.out.println(num1+" and "+num2+" are twin-prime numbers.");
else
System.out.println(num1+" and "+num2+" are not twin-prime numbers.");
}
}
Ourput-1:
Enter 2 number
13
13
13 and 13 are twin-prime number
output-2:
Enter 2 number
7
13
7 and 13 are not twin-prime number
output-3:
Enter 2 number
10
20
10 and 20 are twin-prime number
Exaplanation:
- first i wrote normal code for checking prime or not but i added som extra checking in for loop
- for normal prime number we check i <=num/2 if we have only 1 number to check
- but here we have 2 number so i wrote another condition in for loop using and keyword do that it should be true fro both the conditions then only it enters loops or else not
- so again if it enters loop again i gave and keyword and wrote another condition to check for another number too
- if both numbers passes the condition as true then only the flag variable will be taken true or else it will remain false
- after the loop i will check weather flag is true or not and with flad i will check weather both the numbers are same or not by checking num1 == num2 then only it it will print both numbers are twin prime numbers
-----Hope you liked my program given brainliest if you liked my program and if you have any doubts regarding programming or coding you can ask me i will explain or most of the cases solve your problem :)