# Thread: I don't know whats wrong with this

1. ## I don't know whats wrong with this

I've been working at this code for several days trying to work out Kaprekars constant (see 6174 (number) - Wikipedia, the free encyclopedia for an explanation) and I'm almost there, but the program is not spitting out what I'd like it too.

from what I can tell the code seems correct and I've been going over it all day trying to figure out what the problem is with no success

I know that the problem is located in kaprekar, but I can't find the specific tree within the forest.
Here is the code

Code:
```# include <stdio.h>
int inputVariables[4]; //where userinput goes after being broken up
int i,j;
int userInput, remainders; //used to break up input
int numberX = 0; // users input arranged forwards
int numberY = 0; // users input arranged backwards

//function declaration follows
int sort(int i);
int backsort(int numberX);
int kaprekar(int userInput);

main ()
{
userInput = 1000;
kaprekar(userInput);
}

int sort( int userInput)
{

remainders = userInput % 1000;
inputVariables[0] = userInput / 1000;
userInput = remainders;

remainders = userInput % 100;
inputVariables[1] = userInput / 100;
userInput = remainders;

remainders =userInput % 10;
inputVariables[2] = userInput / 10;
userInput = remainders;
inputVariables[3] = remainders;

for (i = 0; i < 3; ++i)
{
for (j = 0; (j < 4 - 1 - i); ++j)
{
if (inputVariables[j] > inputVariables[j+1])
{
remainders = inputVariables[j+1];
inputVariables[j+1] = inputVariables[j];
inputVariables[j] = remainders;
}
}
}

numberX = (inputVariables[0]*(1000))+(inputVariables[1]*(100))+(inputVariables[2]*(10))+(inputVariables[3]);

return numberX;

}

int backsort(int numberX)
{
remainders = userInput % 1000;
inputVariables[0] = userInput / 1000;
userInput = remainders;

remainders = userInput % 100;
inputVariables[1] = userInput / 100;
userInput = remainders;

remainders =userInput % 10;
inputVariables[2] = userInput / 10;
userInput = remainders;
inputVariables[3] = remainders;

numberY = (inputVariables[0]*(1))+(inputVariables[1]*(10))+(inputVariables[2]*(100))+(inputVariables[3]*(1000));

return numberY;
}

int kaprekar(int userinput)
{

for (i = 1000; i <=1001; i++)
{

sort(i);
backsort(numberX);
j = 1;
if (i % 20 == 0)
{
printf("\f");
}

do
{
if(j>7)
{break;}

if (numberX == 6174 | numberY ==6174)
{
printf("6174 \n");
}
do {

if (numberX > numberY)
{

userInput = numberX-numberY;
printf ("%d \t ", userInput);
j++;
}
else
{
userInput = numberX-numberY;
printf ("%d \t ", userInput);
j++;
}
if (numberX == numberY)
{
break;
}

} while ((numberY > numberX)| (numberX > numberY));

if (userinput != 6174)
{
sort(userInput);
backsort(numberX);
j++;
}

}while (userinput != 6174);

if (numberX == numberY)
{continue;}
}
}```
please keep in mind that program currently loops indefinitely

2. Well first of all, line 134 has an error, because | should not be a valid C operator. You were probably wanting to use the logical OR operator, which is ||. Also, try making your functions in a better order. Sort should be in front of kaprekar because it is called 2nd, and then you would need the last function at the end, since it would make sense if they were organized from top to bottom.

3. Well never mind... I forgot that | is bitwise OR, I still think you should use logical OR though.

4. Originally Posted by Fear_Impurity
from what I can tell the code seems correct and I've been going over it all day trying to figure out what the problem is with no success

I know that the problem is located in kaprekar, but I can't find the specific tree within the forest.
One big problem is that your current code is a big mess (no consistent indentation, global variables) which makes it hard to read and to follow the flow. I'm personally not in the mood right now to dig into it.

Just one tip: Use a debugger and set breakpoints at strategic lines (e.g. at the beginning of a loop) to find out where and why the values don't change as expected.

Bye, Andreas