Write a C program whether the given number is Perfect number or not.

Definition of Perfect number: A positive integer n is called a perfect number if it is equal to the sum of all of its positive divisors, excluding n itself.


For example, 6 is perfect integer number, because 1, 2 and 3 are its proper positive divisors and 1+2+3=6.

The next perfect number is 28 because 1+2+4+7+14=28.
The next perfect number is 496 because
1+2+4+8+16+31+62+124+248=496.
/*program to check whether a number is perfect or not*/

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

Output:-
Enter a number : 7543
Given number is Not Perfect Number

Enter a number : 8128
Given number is Perfect Number

No comments:

Post a Comment