write a program to check wheather a number is perfect number or not
Answers
Answered by
4
◆Hey There◆
____________________________________
#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)
sum = sum + i + n/i;
// If sum of divisors is equal to
// n, then n is a perfect number
if (sum == n && n != 1)
return true;
return false;
}
// Driver program
int main()
{
cout << "Below are all perfect numbers till 10000n";
for (int n =2; n<10000; n++)
if (isPerfect(n))
cout << n << " is a perfect numbern";
return 0;
}
____________________________________
#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)
sum = sum + i + n/i;
// If sum of divisors is equal to
// n, then n is a perfect number
if (sum == n && n != 1)
return true;
return false;
}
// Driver program
int main()
{
cout << "Below are all perfect numbers till 10000n";
for (int n =2; n<10000; n++)
if (isPerfect(n))
cout << n << " is a perfect numbern";
return 0;
}
Answered by
1
It is a no which is equal to the sum of its factors other than the no itself.
E.g. 6=1+2+3
For this first of all find the sum of factors and then check whether it is equal to the entered number.
Hope you have understood the logic.
Pls Don't forget to mark this answer as brainliest.
E.g. 6=1+2+3
For this first of all find the sum of factors and then check whether it is equal to the entered number.
Hope you have understood the logic.
Pls Don't forget to mark this answer as brainliest.
Similar questions