- #1
Valour549
- 57
- 4
First of all there are plenty of answers on Google for this commonly searched problem, but none of them gives me the solution I am looking for.
What I am looking for is how to write a function which determines whether a parameter number is a perfect-number. Take the code below for example, notice how the function perfect doesn't actually tell you whether number is a perfect-number or not. All it does is add all the divisors of number and put it into sum. The part of the code that actually determines whether number is a perfect-number or not is inside the function main.
In other words, I am wanting to know how to put the actual determination (starting with... if(re==n)...) inside the function perfect, which I'm strugging with since my knowledge of function is that they end with return {expression} (if they do indeed return a value), or simply return; (if they don't return anything at all).
What I am looking for is how to write a function which determines whether a parameter number is a perfect-number. Take the code below for example, notice how the function perfect doesn't actually tell you whether number is a perfect-number or not. All it does is add all the divisors of number and put it into sum. The part of the code that actually determines whether number is a perfect-number or not is inside the function main.
In other words, I am wanting to know how to put the actual determination (starting with... if(re==n)...) inside the function perfect, which I'm strugging with since my knowledge of function is that they end with return {expression} (if they do indeed return a value), or simply return; (if they don't return anything at all).
Code:
#include<iostream>
using namespace std;
int perfect(int number)
{
int sum=0;
for(int i=1;i<number;i++)
{
if(number%i==0)
{
sum=sum+i;
}
}
return sum;
}
void main()
{
int n;
cout<<"Enter a number: ";
cin>>n;
int re= perfect(n);
if(re==n)
cout<<"\n\nYes!\n It Is A perfect number.\n\n";
else
cout<<"\n\nNo! \n It Is Not A Perfect Number.\n\n";
}