Home » C++ programming language

C++ program to find perfect number

Learn: What is perfect number? How to check whether a given number is perfect number of not?
Submitted by Manu Jemini, on September 28, 2017

Given an integer number and we have to check whether it is perfect number or not?

This C++ program is used to find the perfect number of a positive number and find its all positive divisors excluding that number.

Explanation: For Example, 6 is a perfect number since divisors of 6 are 1, 2, and 3, then sum of its divisor is 1 + 2 + 3 = 6.

Note: A perfect number is a positive integer that is equal to the sum of its proper positive divisors.

Consider the program:

#include <iostream> 
using namespace std; 

int main()
{
	int n,i=1,sum=0;

	cout<<"Enter a number: ";
	cin>>n;
	while(i<n)
	{
		if(n%i==0)
			sum=sum+i;
		i++; 
	}

	if(sum==n)
		cout << i << " is a perfect number\n"; 
	else
		cout << i << " is not a perfect number\n";

	return 0;
}

Output

Enter a number: 6
6 is a perfect number





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.