C++ Program to Check Perfect Number

In this post, we will learn how to check the perfect number using C++ Programming language.

A Perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example: The divisors of 6 are 1, 2 and 3 such that 1 + 2 + 3 = 6. Hence, 6 is a perfect number.

We will be using following methods to check a perfect number:

  1. Using for loop
  2. Using while loop
  3. Using functions

So, without further ado, let’s begin this tutorial.

C++ Program to Check Perfect Number

// C++ Program to Check Perfect Number
#include <iostream>
using namespace std;

int main(){
    int i, num, sum = 0;
    
    // Asking for input
    cout << "Enter any number: ";
    cin >> num;
    
    // Calculating sum of factors
    for (i = 1; i < num; i++){
        if (num % i == 0){
            sum = sum + i;
        }
    }
    
    // Checking perfect number
    if (num == sum){
        cout << num << " is a perfect number.";
    }
    else{
        cout << num << " is not a perfect number.";
    }
    return 0;
}

Output 1

Enter any number: 6
6 is a perfect number.

Output 2

Enter any number: 12
12 is not a perfect number.

How Does This Program Work ?

    int i, num, sum = 0;

In this program, we have declared three int data type variables named i, num and sum.

    // Asking for input
    cout << "Enter any number: ";
    cin >> num;

Now, the user is asked to enter any positive number.

    // Calculating sum of factors
    for (i = 1; i < num; i++){
        if (num % i == 0){
            sum = sum + i;
        }
    }

We calculate the sum of all the divisors of the entered number. 

    if (num == sum){
        cout << num << " is a perfect number.";
    }
    else{
        cout << num << " is not a perfect number.";
    }

Now, we check whether the sum of divisors is equal to the number itself or not. If yes, then the entered number is a perfect number otherwise the entered number is not a perfect number.

C++ Program to Check Perfect Number Using While Loop

// C++ Program to Check Perfect Number Using While Loop 
#include <iostream>
using namespace std;

int main(){
    int i = 1, sum = 0, num;
    
    // Asking for input
    cout << "Enter any number: ";
    cin >> num;
    
    // Calculating the sum of divisors
    while (num > i){
        if (num % i == 0){
            sum = sum + i;
        }
        i++;
    }
    
    // Checking perfect number
    if (num == sum){
        cout << num << " is a perfect number.";
    }
    else{
        cout << num << " is not a perfect number.";
    }
    return 0;
}

Output 1

Enter any number: 28
28 is a perfect number.

Output 2

Enter any number: 32
32 is not a perfect number.

C++ Program to Check Perfect Number Using Functions

// C++ Program to Check Perfect Number
#include <iostream>
using namespace std;

int perfect(int num){
    int i = 1, sum = 0;
    while (num > i){
        if (num % i == 0)
        sum = sum + i;
        i++;
    }
    return(sum);
}

int main(){
    int result, num;
    
    // Asking for input
    cout << "Enter a positive number: ";
    cin >> num;
    
    result = perfect(num);
    
    // Checking perfect number
    if (num == result){
        cout << num << " is a perfect number.";
    }
    else{
        cout << num << " is not a perfect number.";
    }
    return 0;
}

Output 1

Enter any number: 496
496 is a perfect number.

Output 2

Enter any number: 469
469 is not a perfect number.

Conclusion

I hope after going through this post, you understand how to check the perfect number using C++ Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *