Could u please help me solve the foll. question in C++:
Write a program to enter any positive integer & print whether it is a perfect number or not.
(If a number is equal to sum of all its factors it is said to be a perfect number ex.6=1+2+3)
Thankyou for your question.
Well its quiet an interesting problem, well let me
tell your first there are only 4 perfect numbers from
1 to 1000, may be ahead of it also.
Your problem says to find the perfect number.
You should attack this problem like
a) Find the factors of that number. More appropriately
make a function say perfect, which takes an integer
b)Secondly i already told you have to find the factors.
According to maths, a number can a factor of other number
if it is its half.
Umm, example, lets your number is 6, so it can take factors
upto 3, because 3 is 6's half. Plus always remeber 1 is the factor of all numbers so you can write a simple code in your function like
int product = 1 ;
for( int i = 1 ; i <= ( n / 2 ) ; i++ )
// 'n' is the positive number you entered.
//Simply put a check like this now
if( n % i == 0 )
product += i ;
return true ;// the number is perfect.
That is simple like this, i hope i have tried to explain clear. If you have any more queries, dont be afraid to ask.
Hope this helps.