View Single Post
Old Mar 19th, 2008, 5:50 PM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
Re: Why my program dont cout perfect number?

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
               sum += i;
            }
            if (sum == rob) { 
               return true;
            } else {
               return false;       
            }
        }
return true;
}

is wrong for a start

the if sum == rob statement should be outside of the for loop, otherwise your exiting after the first iteration, you need that loop to finish calculating sum before you do this if check.

for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }

I don't understand why you need to check if n is a perfect number n amount of times. Perhaps you should look at your code and understand whats happening.
Seif is offline   Reply With Quote