Thursday, 11 February 2016

find whether a given number (say x) is a “perfect number” or not.

Definition of Perfect number:
A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself. By this definition, 1 is not a perfect number.

Explanation:
Take number 6.

Proper positive divisors of 6 is 1,2,3 and their sum is 1+2+3=6.
So, 6 is a perfect number.

Solution
#include<stdio.h> int main() { int a, b; scanf("%d", &a); b=a; int sum=0, i; for(i=1; i<a; i++) { if((a%i)==0) { sum=sum+i; } } if(sum==b) printf("yes"); else printf("no"); return 0; }

No comments:

Post a Comment