Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class E07PrimersBessons {
- public static void main(String[] args) {
- Scanner lector = new Scanner(System.in);
- int num1;
- int num2;
- int cont = 0;
- boolean flag;
- do {
- System.out.println("Introduce un numero:");
- flag = lector.hasNextInt();
- if (!flag) {
- lector.nextLine();
- System.out.println("El numero tiene que ser entero");
- }
- } while (!flag);
- num1 = lector.nextInt();
- lector.nextLine();
- do {
- System.out.println("Introduce otro numero:");
- flag = lector.hasNextInt();
- if (!flag) {
- lector.nextLine();
- System.out.println("El numero tiene que ser entero");
- }
- } while (!flag);
- num2 = lector.nextInt();
- lector.nextLine();
- for (int i = num1; i > 0; i--) {
- if (num1 % i == 0) {
- cont++;
- }
- }
- if (cont == 2 && (num1+2==num2 || num1-2==num2)) {
- cont=0;
- for (int j = num2; j > 0; j--) {
- if (num2 % j == 0) {
- cont++;
- }
- }
- if (cont==2){
- System.out.println("Los numeros "+ num1+" y "+num2+" son primos gemelos.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement