RAUL-SUAREZ

tp0_ejercicio_03

Aug 30th, 2022
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Ejercicio_03 {
  5.  
  6.     public static void main(String[] args) {
  7.         Helper obj = new Helper();
  8.         Scanner entrada = new Scanner(System.in);
  9.         Random aleatorio = new Random();
  10.         int num = 0;
  11.  
  12.         int opc = obj.getInteger(entrada,
  13.                 "Elija la opcion de ingreso de datos:\n1 = Manual" + "\n2 = Aleatorio" + "\n ");
  14.  
  15.         switch (opc) {
  16.         case 1:
  17.             do {
  18.                 num = obj.getInteger(entrada, "Ingrese un Numero Positivo: ");
  19.             } while (num < 0);
  20.  
  21.             break;
  22.         case 2:
  23.             num = aleatorio.nextInt(20);
  24.             System.out.println("Numero Aleatorio: " + num);
  25.         }
  26.         System.out.println("¿El numero es PERFECTO, ABUNDANTE O DEFICIENTE? ");
  27.         validarPerfecto(num);
  28.         validarAbundante(num);
  29.         validarDeficiente(num);
  30.  
  31.     }
  32.  
  33.     public static void validarPerfecto(int n) {
  34.         int suma = 0;
  35.         for (int i = 1; i < n; i++) {
  36.             if (n % i == 0) {
  37.                 suma += i;
  38.             }
  39.         }
  40.         if (suma == n) {
  41.             System.out.println("¡¡¡ES UN NUMERO PERFECTO!!!");
  42.         } else {
  43.             System.out.println("no es perfecto");
  44.         }
  45.     }
  46.  
  47.     public static void validarAbundante(int n) {
  48.         int suma = 0;
  49.         for (int i = 1; i < n; i++) {
  50.             if (n % i == 0) {
  51.                 suma += i;
  52.             }
  53.         }
  54.         if (suma > n) {
  55.             System.out.println("¡¡¡ES UN NUMERO ABUNDANTE!!!");
  56.         } else {
  57.             System.out.println("no es abundante");
  58.         }
  59.     }
  60.  
  61.     public static void validarDeficiente(int n) {
  62.         int suma = 0;
  63.         for (int i = 1; i < n; i++) {
  64.             if (n % i == 0) {
  65.                 suma += i;
  66.             }
  67.         }
  68.         if (suma < n) {
  69.             System.out.println("¡¡¡ES UN NUMERO DEFICIENTE!!!");
  70.         } else {
  71.             System.out.println("no es deficiente");
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment