Ok so i have to write a program that tells the user if the number entered is a p
ID: 669730 • Letter: O
Question
Ok so i have to write a program that tells the user if the number entered is a perfect number, so far so good. I also have to have it tell the users waht iteration they are on as well as ask if they awnt to continue yes or no. Not Sure how to do that
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n, num, sum = 0;
printf("Enter a perfect number: ");
scanf_s("%d", &num);
n = num;
for (i = 1; i<n; i++)
{
if (num%i == 0)
sum = sum + i;
}
if (num == sum)
printf("Number %d is perfect. ", num);
else
printf("Number %d is not perfect. ", num);
return 0;
}
Explanation / Answer
#include<stdio.h>
int main(){
int n,i=1,sum=0;
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);
return 0;
}
output:
Enter a number: 6
6 is a perfect number
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.