C++ Program to check a number is perfect number or not


In this program, You will learn how to check a number is a perfect number or not in C++.


if (n % i == 0) {

s = s + i; }


Example: How to check a number is perfect number or not in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int n, i = 1, s = 0;
    cout << "Enter A Number :";
    cin>>n;

    while (i < n) {
        if (n % i == 0) {
            s = s + i;
        }
        i++;
    }

    if (s == n) {
        cout << "\nThis is a Perfect Number :" << n;
    } else {
        cout << "\nThis is Not a Perfect Number :" << n;
    }

    return 0;
}

Output:

Enter A Number :28
This is a Perfect Number :28