Saturday, December 5, 2015

Uri problem 1164

Description:

Perfect Number

Adapted by Neilor Tonin, URI  Brazil
Timelimit: 1
In mathematics, a perfect number is an integer for which the sum of all its own positive divisors (excluding itself) is equal to the number itself. For example the number 6 is perfect, because 1+2+3 is equal to 6. Your task is to write a program that read integer numbers and print a message informing if these numbers are perfect or are not perfect.

Input

The input contains several test cases. The first contains the number of test cases N (1 ≤ N ≤ 100). Each of the following N lines contain an integer X (1 ≤ X ≤ 108), that can be or not a perfect number.

Output

For each test case output the message “X eh perfeito” (X is perfect) or “X nao eh perfeito” (X isn't perfect) according with to above specification.
Sample InputSample Output
3
6
5
28
6 eh perfeito
5 nao eh perfeito
28 eh perfeito
Solution:

#include<stdio.h>
int main()
{
    int x , i, T, sum=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&x);
        for(i=1; i<x; i++){
            if(x%i==0){
                sum+=i;
            }
        }
        if(sum==x)
            printf("%d eh perfeito\n",x);
        else
            printf("%d nao eh perfeito\n",x);
        sum=0;
    }

    return 0;
}

No comments:

Post a Comment