Sunday, January 27, 2013

Perfect Number Logic

/*
Program to check the given number is perfect or not
OS: UNIX or Windows

*/

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

/*
 Refer these links to get the clear idea about Perfect numbers

http://mathworld.wolfram.com/PerfectNumber.html

Perfect number 'n' is a positive number where sum of divisors of n is equal to n.

Ex: number is 6, its divisors are 1,2,3 there fore sum of 1+2+3=6

*/

No comments:

Post a Comment