A number is said to be perfect if the sum of its divisors (except for itself) is
ID: 3768002 • Letter: A
Question
A number is said to be perfect if the sum of its divisors (except for itself) is equal to itself. For example, 6 is a perfect number because the sum of its divisors (1 + 2 + 3) is 6. The number 8 is said to be deficient because the sum of its divisors (1 + 2 + 4) is only 7. The number 12 is said to be abundant because the sum of its divisors (1 + 2 + 3 + 4 + 6) is 16. Write a program that lists the factors of the numbers between 1 and 100 and classifies each number as perfect, deficient, or abundant.
Explanation / Answer
#include <stdio.h>
#include<conio.h>
int main()
{
int n,i,sum=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factors of %d are: ", n);
for(i=1;i<n;++i)
{
if(n%i==0){
printf("%d ",i);
sum=sum+i;}
}
if(sum==n)
{
printf("%d is a perfect number",n);
}
else {if(sum<n){
printf("%d is a deficient number",n);}
else{
printf("%d is an abundant number",n);}}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.