Advertisement
mendigo

Untitled

Aug 12th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. public class NumeroPerfeito {
  2.  
  3.     public static void main(String[] args) {
  4.         int n = -1;
  5.         do {
  6.             n = Console.lerInt(new String("Digite um número: "));
  7.             if ( Perfeito(n) )
  8.                 Console.escrever(new String("O número "+n+" é um número perfeito."));
  9.             else
  10.                 Console.escrever(new String("O número "+n+" não é um número perfeito."));
  11.             Console.pulaLinha();
  12.         } while (n != 0);
  13.     }
  14.     private static boolean Perfeito(int n)
  15.     {
  16.         int soma=0,contador=1;
  17.         boolean perfeito=false;
  18.         for( contador=1;contador<n;contador++){
  19.             if(n%contador==0){
  20.                 soma+=contador;
  21.             }if (soma==n){
  22.                 perfeito=true;
  23.             }
  24.         }
  25.         return perfeito;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement