views:

48

answers:

2

I have this code from my professor and it's about finding abundant and defective numbers. A number x is effective if the sum of all integer divisors, except for x itself, is less than x. If it's greater than x, then it's abundant. There are no compile errors and it seems all fine, but it just doesn't print anything, it doesn't get any result. Why ? What's wrong?

#include <stdio.h>

int main(void)
{
        int x=1,y=1;
        int sum=0;
        while(x<100) 
        {
                while(y<x) 
                {
                        if(x%y==0) {
                                sum=sum+y;
                                y++;
                        }
                }
                x++;
                sum=0;
                y=1;
        }
        return 0;
}

If you can help me here, I would be very grateful, thanks in advance.

+6  A: 

Your code has no print statements. You probably want to use the printf function.

Michael McGowan
+1 for spotting the obvious that I didn't :-)
pmg
As dark_charlie pointed out, you also want to make sure you avoid infinite loops by incrementing y outside of the if.
Michael McGowan
I lol'd a little
dfens
+2  A: 

The problem is here:

        while(y<x) 
        {
                if(x%y==0) {
                        sum=sum+y;
                        y++;
                }
        }

Y should be advanced every iteration:

            while(y<x) 
            {
                    if(x%y==0) {
                            sum=sum+y;
                    }
                    y++;
            }
dark_charlie