Write a c program to display perfect number from 1 to 100
Answers
#include<stdio.h>
int main(){
int sum=0,p,i;
printf("\n Perfect numbers between 1 and 100 are: ");
for(i= 1; i<= 100; i++){
p=1;
while(p<=(i/2)){
if(i % p == 0)
sum=sum+p;
p++;
}
if(sum==i)
printf(" %d ",i);
sum=0;
}
return 0;
}
, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Answer:
#include <iostream>
using namespace std;
int main()
{
cout << "\n\n Find the perfect numbers between 1 and 100:\n";
cout << "------------------------------------------------\n";
int i = 1, u = 1, sum = 0;
cout << "\n The perfect numbers between 1 to 100 are: \n";
while (i <= 100)
{
while (u <= 100)
{
if (u < i)
{
if (i % u == 0)
sum = sum + u;
}
u++;
}
if (sum == i) {
cout << i << " " << "\n";
}
i++;
u = 1;
sum = 0;
}
}
Explanation:
Compile and execute to check results