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.(adsbygoogle = window.adsbygoogle || []).push({});

What I am looking for is how to write a function which determines whether a parameternumberis a perfect-number. Take the code below for example, notice how the functionperfectdoesn't actually tell you whethernumberis a perfect-number or not. All it does is add all the divisors ofnumberand put it intosum. The part of the code that actually determines whethernumberis a perfect-number or not is inside the functionmain.

In other words, I am wanting to know how to put the actual determination (starting with...if(re==n)....) inside the functionperfect, which I'm strugging with since my knowledge of function is that they end withreturn {expression}(if they do indeed return a value), or simplyreturn;(if they don't return anything at all).

Code (C):#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";

}

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Writing a function that determines if a number is perfect

Loading...

Similar Threads - Writing function determines | Date |
---|---|

C/++/# Writing a recursive function by using optional parameters | Jul 24, 2016 |

Fortran help: Open and Write functions | Apr 25, 2013 |

Error writing recursive function for Bonnet's recursion formula in C | Jan 9, 2012 |

Writing A Print Function | Jul 23, 2011 |

MIPS - write a function to remove blanks | Nov 2, 2008 |

**Physics Forums - The Fusion of Science and Community**