Program to print perfect numbers between 1 to 100
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int number = 1, counter = 0, sum = 0;
cout
<< "\n\n\t Program to print perfect numbers between 1 to
100\n\n";
cout
<< "\n\n\t Perfect Numbers - ";
for(number = 1 ; number <= 100; number++)
{
sum
= 0;
for(counter = 1; counter < number; counter++)
{
if ((number % counter) == 0)
{
sum
= sum + counter;
}
}
if (sum == number)
{
cout << number << " , ";
}
}
cout
<< "\b\b" << " " ;
getch();
return 0;
}
An integer is said to be a perfect number if the sum of the its factor including 1 (but not the number itself) is equal to its number. For Example - 6 is a perfect number, because 6 = 1 + 2 + 3.
No comments:
Post a Comment