miércoles, 5 de febrero de 2014

Calcular los números defectivos hasta el leído

public static void main(String[] args) {
        Scanner leo = new Scanner (System.in);
        int suma,numero=0;
        boolean incorrecto;
       
        do
            try{
                System.out.println("Introduzca un número");
                numero= leo.nextInt();
                incorrecto=false;
            }
            catch(Exception e){incorrecto=true;}
        while(incorrecto);
        


        for (int i = 1; i <=numero; i++) {
            suma=0;
            System.out.print(i+ ": ");
            for (int j = 1; j < i; j++) {
                if (i%j==0) {
                    suma+=j;
                    System.out.print(j+", ");
                }
            }
            System.out.println();
            if (suma<i) System.out.println("    " +i + " es defectivo");
        }
       
       
    }

No hay comentarios: