# Thread: Help on a function.

1. ## Help on a function.

Same program I was asking for help on earlier. Got it to work just fine except for this part.

Here's the whole function.

Code:
```// Returns single character
char FindSum(int x)
{
int y, z, sum = 0;
char Class;
for (y=1; y<x && y % 1 == 0; y++)
{
z = x / y;

if (x % y == 0)
sum += z;
}

if (sum == x)
Class = 'P';
else if (sum > x)
Class = 'A';
else
Class = 'D';

return Class;
}```
The idea is to classify numbers as abundant, deficient, or perfect. However, I can't fin the flaw in my logic that is turning up every number I punch in (including a known perfect number like 6) as abundant.

Can anyone help?

2. > However, I can't fin the flaw in my logic
So debug it then.

Here's an idea.
Code:
```// Returns single character
char FindSum(int x)
{
int y, z, sum = 0;
char Class;
for (y=1; y<x && y % 1 == 0; y++)
{
z = x / y;
if (x % y == 0)
sum += z;
printf("x=%d, y=%d, z=%d, x%%y=%d, sum=%d\n", x, y, z, x%y, sum);
}

if (sum == x)
Class = 'P';
else if (sum > x)
Class = 'A';
else
Class = 'D';

return Class;
}```
My results for calling it with 6
Code:
```\$ gcc foo.c
\$ ./a.out
x=6, y=1, z=6, x%y=0, sum=6
x=6, y=2, z=3, x%y=0, sum=9
x=6, y=3, z=2, x%y=0, sum=11
x=6, y=4, z=1, x%y=2, sum=11
x=6, y=5, z=1, x%y=1, sum=11
A```

3. y%1 is always zero.

Popular pages Recent additions