Find The Perfect Number In C++


Levels of difficulty: / perform operation:

What is a perfect number?
“Perfect number is a positive number which sum of all positive divisors excluding that number.”
For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and 28 is also a Perfect Number
since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128



C++ Program

#include<iostream.h>
#include<conio.h>
void main() {
	clrscr();
	int i=1, u=1, sum=0;
	while(i<=500) {
		while(u<=500) {
			if(u<i) {
				if(i%u==0 )
				      sum=sum+u;
			}
			u++;
		}
		if(sum==i) {
			cout<<i<<" is a perfect number."<<"\n";
		}
		i++;
		u=1;
		sum=0;
	}
	getch();
}


Output

6 is a perfect number.
28 is a perfect number.
496 is a perfect number.





Quantitative Aptitude
Reasoning
Programming
Interview