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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.