C++ Program to Check Whether a Given Number is Perfect Number


This is a C++ Program to Check if a Number is a Perfect Number.

Problem Description

The program intakes a number and checks if it is a perfect number or not. A perfect number is equal to the sum of its divisors excluding itself.

Problem Solution

1. The number to be checked if it is perfect or not is entered.
2. The divisors of the number are calculated and added.
3. If the sum of the divisors is equal to the original number, then it is a perfect number.
4. Else the given number is not a perfect number,
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Check if a Number is a Perfect Number. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {  
  5.     int i, num, div, sum=0;
  6.     cout << "Enter the number to be checked : ";
  7.     cin >> num;
  8.     for (i=1; i < num; i++)
  9.     {
  10.         div = num % i;
  11.         if (div == 0)
  12.             sum = sum + i;
  13.     }
  14.     if (sum == num)
  15.         cout << "\n" << num <<" is a perfect number.";
  16.     else
  17.         cout << "\n" << num <<" is not a perfect number.";
  18.     return 0;
  19. }
Program Explanation

1. The user is asked to enter the number to be checked and it is stored in the variable ‘num’.
2. Initialize the variable ‘sum’ as 0.
3. A ‘for’ loop is used to find the factors of the given number.
4. Using the modulus operator, the divisors are calculated.
5. If the remainder is 0, it is added to the variable ‘sum’.
6. The loop continues till it reaches num-1.
7. Now, if ‘sum’ equals the entered number ‘num’, then it is a perfect number.
8. Else the entered number is not a perfect number.
9. The result is then printed.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
Case 1 :
Enter the number to be checked : 28
28 is a perfect number.
Case 2 :
Enter the number to be checked : 16
16 is not a perfect number.
Case 3 :
Enter the number to be checked : 6
6 is a perfect number.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Take C++ Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.