DEV Community

realNameHidden
realNameHidden

Posted on

Perfect Number Program in C

For explanation watch video

Perfect Number :
In mathematics, a perfect number is a positive
integer that is equal to the sum of its positive divisors, excluding the number itself.
ex : 6 = 1,2,3 < 6

1+2+3 =6
ex: 5 : 1
sum = 1 !=5
5 is not perfect number
Perfect Numbers examples : 6,28,496,8128

Program to check given number is perfect or not:

#include<stdio.h>
int main(){
    int n;
    printf("Enter the number: ");
    scanf("%d",&n);
    int sum = 0,i;
    for(i=1;i<n;i++){
        if(n%i==0){
            sum = sum + i;
        }
    }
    if(sum == n){
        printf("%d is a perfect Number",n);
    }else{
        printf("%d is not perfect Number",n);
    }
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)