Computer Science, asked by khushikumari711089, 2 months ago

Write a program to accept an integer, compute and check whether it is a perfect number or not. A number is
known as perfect number, if the sum of factors (excluding the number) is equal to the number, eg. 6=1+2+3.​

Answers

Answered by mjeystech
0

Answer:

// C++ program to check if a given number is perfect or not

Explanation:

#include<iostream>

using namespace std;

// Returns true if n is perfect

bool isPerfect(long long int n)

{

// To store sum of divisors

long long int sum = 1;

// Find all divisors and add them

for (long long int i=2; i*i<=n; i++)

{

if (n%i==0)

{

if(i*i!=n)

sum = sum + i + n/i;

else

sum=sum+i;

}

}

// If sum of divisors is equal to

// n, then n is a perfect number

if (sum == n && n != 1)

return true;

return false;

}

int main()

{

cout << "Below are all perfect numbers till 10000\n";

for (int n =2; n<10000; n++)

if (isPerfect(n))

cout << n << " is a perfect number\n";

return 0;

}

Similar questions