





Please login
Prime

Prepinsta Prime
Video courses for company/skill based Preparation
(Check all courses)
Get Prime Video
Prime

Prepinsta Prime
Purchase mock tests for company/skill building
(Check all mocks)
Get Prime mock
C++ Program to check whether a number is Perfect Number or not
Program to check whether a number is Perfect Number or not
Here we will discuss how to check whether a number is Perfect number or not using C++ Programming language.
Perfect Number is a number in which the sum of the proper positive divisors of the number is equal to the number itself.
To check for Perfect Number find and add the divisors of the number and compare, if both are equal then it is a Perfect Number else it is not a Perfect Number.
For Example: 28
Sum of divisors = 1 + 2 + 4 + 7 + 14
= 28


Algorithm:-
- User gives an input
- Input is stored in an int type variable say num.
- divisors of num can be found in range 1 to num
- Initialize sum to 0
- A Loop is traversed from i=1 to num
- check wether i is divisor of num or not using num%i==0.
- If i is divisor of num then update sum=sum+i.
- the loop will works until all the divisors are not added.
- check wether i is divisor of num or not using num%i==0.
- sum is compared with num
- if both are equal then the number is a Perfect number
- otherwise then number is not a Perfect Number
C++ Code:-
//C++ Program
//Perfect Number or not
#include<iostream>
using namespace std;
//main Program
int main ()
{
int div, num, sum=0;
cout << “Enter the number to check : “;
//user input
cin >> num;
//loop to find the sum of divisors
for(int i=1; i < num; i++)
{
div = num % i;
if(div == 0)
sum += i;
}
//checking for perfect number
if (sum == num)
cout<< num <<” is a perfect number.”;
else
cout<< num <<” is not a perfect number.”;
return 0;
}
Output
Enter the number to check : 28
28 is a perfect number.
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java

Login/Signup to comment