/* THE GIVEN NUMBER IS PERFECT NUMBER OR NOT */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,sum=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
{
sum=sum+i;
}
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
getch();
}
EXAMPLES:
For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,sum=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
{
sum=sum+i;
}
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
getch();
}
EXAMPLES:
For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128
No comments:
Post a Comment