Kaprekar algorithm help.

0 Scorpion Hunter · March 2, 2015





#include 
int main(void)

{

int number = 0;
int lowNumber = 0;
int highNumber = 0;
int digit1 = 0, digit2 = 0, digit3 = 0, digit4 = 0;


printf("\n- - - K A P R E K A R  A L G O R I T H M - - -\n");
printf("\nprocess a single 4-digit number, or a ranger.\n");
printf("\nEnter single [xxxx] or range [xxxx-xxxx] ==> ");
scanf("%d", &number);


//splits a 4-digit number into its 4 digits.

number = (digit1 / 1000) % 10;
number = (digit2 / 100) % 10;
number = (digit3 / 10) % 10;
number = (digit4 / 1) % 10;

//sorts 4 digits into ascending order.

if (digit1 > number)

number = digit1;

if (digit2 > number)

number = digit2;

if (digit3 > number)

number = digit3;

if (digit4 > number)

number = digit4;


//joins 4 digits into one 4-digit number

scanf("%d%d%d%d", &digit1, &digit2, &digit3, &digit4);

//performs one step of the Kaprekar algorithm

number = 10 + 1 - 4;
number = 10 + 2 - 1 - 3;
number = 3 - 1 - 2;
number = 4 - 1;

// print line containing the sequence

printf("Kaprekar Sequence is %d\n" );

//prints the grid corresponding to the low/high range.

printf("+----------+\n");
printf("|          |\n");
printf("|          |\n");
printf("|          |\n");
printf("|          |\n");
printf("+----------+\n");

return 0;








}


Post a Reply

Replies

Oldest  Newest  Rating
0 Filip Petrovic · March 7, 2015
1. You can't enter something like 4455-4433, the program doesn't understand that....you need "for loop" for testing range between two numbers.
2.Your 4 digit spliting does not make any sense at all...it is supposed to look like this:
digit1 = (number / 1000);
digit2 = (number % 1000) / 100;
digit3 = (number % 100) / 10;
digit4 = (number % 10);

3.Sorting does not work....you can find on the internet how to sort.
4.if you write scanf("%d%d", &digit1, &digit2), that means you have to insert 2 numbers, and first number will be saved in the integer digit1, and the other one in the integer digit2.....if you want to combine 4 integers into a number, you would write:
number = digit1*1000 + digit2*100 + digit3*10 + digit4;
5. If you want to put your numbers into the grid, you need to use two "for loops".....Bucky showed it in 28. tutorial

I wish i could help you more on this, but i really don't understand what exactly do you want your program to do.
  • 1

C

106,983 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator