Week - 1(Assinment -2 )
Write a program to 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.
So, 6 is a perfect number.
Constraint:
1<=x<=1,000,000
1<=x<=1,000,000
Input: A single positive number x
Output:
yes if given number x is a perfect number
yes if given number x is a perfect number
no if given number x is not a perfect number
#include<stdio.h>
int main()
{
int f,sum=0,i;
scanf("%d",&f);
for(i=1;i<=(f/2);i++)
{
if(f%i==0)
{
// printf("%d",sum);
sum+=i;
}
}
if(sum==f)
printf("yes");
else
printf(("no"));
return 0;
}
No comments:
Post a Comment