Fri Nov 01 2019
Perfect Number
C++ Programming1145 views
File Name: perfect-number.cpp
#include<iostream>
using namespace std;
int main() {
int no, i = 1, sum = 0;
/* 'endl' insert linefeed like '\n' */
cout << "Check the given number is Perfect Number or not" << endl;
cout << "Enter a number:" << endl;
cin >> no;
/* Loop the process using "while loop" */
while(i < no) {
if(no % i == 0)
sum += i;
i++;
}
if(sum == no)
cout << no << " is a Perfect Number!" << endl;
else
cout << no << " is not a Perfect Number!" << endl;
return 0;
}
/* Output */
/* Check the given number is Perfect Number or not
Enter a number:
6
6 is a Perfect Number!
*/
/* ------------------------------- */
/*
Check the given number is Perfect Number or not
Enter a number:
14
14 is not a Perfect Number! */
Reference:
Author:Geekboots