Here we will learn how to program using C++ in order to find the perfect number.
A Perfect Number is a number that has equal factors.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include<iostream> #include<iomanip> using namespace std; int main(){ int n,i=1,sum=0; cout<<"Enter a number: "; cin >> n; while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) cout << i << " is a perfect number"; else cout << i << " is not a perfect number"; system("pause"); return 0; } |