Computer Science, asked by INDIAN04, 1 year ago

java program with scanner to input two no.s and find whether they are twin prime or not​

Answers

Answered by rakeshchennupati143
2

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:

  1. first i wrote normal code for checking prime or not but i added som extra checking in for loop
  2. for normal prime number we check i <=num/2 if we have only 1 number to check
  3. 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
  4. so again if it enters loop again i gave and keyword and wrote another condition to check for another number too
  5. if both numbers passes the condition as true then only the flag variable will be taken true or else it will remain false
  6. 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   :)

Similar questions