c++ check if the number is equal to the sum of its divisors excluding itself code example

Example: c++ check if the number is equal to the sum of its divisors excluding itself

// not my code but I found it very useful so I just pasted it
// in order to help others

int i, num, div, sum=0;
    cout << "Enter the number to be checked : ";
    cin >> num;
    for (i=1; i < num; i++)
    {
        div = num % i;
        if (div == 0)
            sum = sum + i;
    }
    if (sum == num)
        cout << "\n" << num <<" is a perfect number.";
    else
        cout << "\n" << num <<" is not a perfect number.";

Tags:

Php Example