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
please keep in mind that program currently loops indefinitelyCode:# 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;} } }



LinkBack URL
About LinkBacks



