Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class E07PrimersBessons {
  4. public static void main(String[] args) {
  5. Scanner lector = new Scanner(System.in);
  6. int num1;
  7. int num2;
  8. int cont = 0;
  9. boolean flag;
  10. do {
  11. System.out.println("Introduce un numero:");
  12. flag = lector.hasNextInt();
  13. if (!flag) {
  14. lector.nextLine();
  15. System.out.println("El numero tiene que ser entero");
  16. }
  17. } while (!flag);
  18. num1 = lector.nextInt();
  19. lector.nextLine();
  20. do {
  21. System.out.println("Introduce otro numero:");
  22. flag = lector.hasNextInt();
  23. if (!flag) {
  24. lector.nextLine();
  25. System.out.println("El numero tiene que ser entero");
  26. }
  27. } while (!flag);
  28. num2 = lector.nextInt();
  29. lector.nextLine();
  30. for (int i = num1; i > 0; i--) {
  31. if (num1 % i == 0) {
  32. cont++;
  33. }
  34. }
  35. if (cont == 2 && (num1+2==num2 || num1-2==num2)) {
  36. cont=0;
  37. for (int j = num2; j > 0; j--) {
  38. if (num2 % j == 0) {
  39. cont++;
  40. }
  41. }
  42. if (cont==2){
  43. System.out.println("Los numeros "+ num1+" y "+num2+" son primos gemelos.");
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement