C++ Program to Check If a Number is a Perfect Number or Not

Perfect numbers

C++ programs to check if a number is a perfect number or not have been shown here. A perfect number is a positive integer that is equal to the sum of its proper divisors. For example 6 is a perfect number because if we add it's proper divisors (1, 2 and 3), we get 6 i.e. 1 + 2 + 3 = 6. Some of the other perfect numbers are 28, 496, 8128 etc.






1. Algorithm to check if a number is a perfect number or not


1. Take a number n as input.

2. Set s = 0, x = 1

3. Check if n is divisible by x

4. If step 3 is true perform s = s + x

5. Set x = x + 1

6. Go step 3 until x > n/2

7. If s = n declare "n is a perfect number"




2. Pseudocode to check if a number is a perfect number or not


Input : A positive number n

Output : n is a perfect number or not

1. Procedure isPerfectNumber(n):

2. s := 0

3. x := 1

4. Repeat until x > (n/2)

5. If n mod x = 0:

6. s := s + x

7. If n = s:

8. Return "Perfect number"

9. Else:

10. Return "Not a perfect number"

11. End Procedure





3. Time complexity to check if a number is a perfect number or not


Time Complexity: O(n)

n is the input number




4. Program & Output to check if a number is a perfect number or not




4.1. C++ Program & Output to check if a number is a perfect number or not using iteration

Code has been copied
/**************************
    alphabetacoder.com
C++ program to check if a
number is a perfect number
***************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int n, i, sum = 0;

    // take input
    cout << "Enter the number: ";
    cin >> n;

    // add all the divisor upto n / 2
    for (i = 1; i <= (n / 2); i++) {
        if (n % i == 0)
            sum = sum + i;
    }

    // check if the input number 
    // is equal to the sum
    if (n == sum)
        cout << n << " is a perfect number\n";
    else
        cout << n << " is not a perfect number\n";

    return 0;
}

Output


Case 1:

Enter the number: 6

6 is a perfect number


Case 2:

Enter the number: 30

30 is not a perfect number




4.2. C++ Program & Output to check if a number is a perfect number or not using recursion

Code has been copied
/***********************************
	  alphabetacoder.com
C++ program to check if a number is 
a perfect number using recursion
************************************/

#include <iostream>

using namespace std;

// recursive function to check if input 
// number is a perfect number or not
int isPerfect(int n, int i, int sum) {
    if (i > n / 2) // exit condition
        return sum == n;

    if (n % i == 0) // check divisibility
        sum += i;

    return isPerfect(n, i + 1, sum);
}

int main() {
    // declare variables
    int n;

    // take input
    cout << "Enter the number: ";
    cin >> n;

    // check if the input number is perfect
    // by calling function
    if (isPerfect(n, 1, 0))
        cout << n << " is a perfect number\n";
    else
        cout << n << " is not a perfect number\n";

    return 0;
}

Output


Enter the number: 28

28 is a perfect number