WAP to check whether a number if perfect number or not.

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. The first perfect number is 6 (1,2,3 are its divisors).
#include<stdio.h>
 #include<stdlib.h>

int main()
{
 int n,i,sum=0;
printf("Enter a number: ");
 scanf("%d",&n);
for(i=1;i<n;i++)
 {        if(n%i==0)
 sum=sum+i;
}
 if(sum==n)
 printf("perfect number");
 else
 printf("not a perfect number\n");
 system("pause");
 return 0;
 }
Advertisements

Leave a comment

Filed under TUTORIALS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s