# Thread: Need help in solving this problem

1. ## Need help in solving this problem

I need to write a C program to print the label alphabets given the label name.
In other words the program should do the following:

Input Output
==== ====
1 A
2 B
----
-----
26 Z
27 AA
28 AB
----
----
51 AY
52 AZ
53 BA
---
---
702 ZZ
703 AAA
704 AAB
etc....

I am unable to come up with a logic... I tried different logics but they are not working. Any idea on how I can proceed.

Regards
Chari

2. We can help you with C, but we can't help you think. You need to figure out how to solve the problem. If I gave you the number 215, can you (personally, yourself) work out what the answer should be?

3. Ok, these kinds of problem scream out to use a while loop, so let's see how it might work with that:

This works just like the odometer on your car, with three being the far right hand "wheel", and two being one wheel to the left of that, and etc.

pseudo-code

Code:
```set char's one, two, and three  to @ //one less than A
get a number from the user, or a file.

while(number > 0)
number--;
three++;
if(three > 'Z')
then increment two and set three = 'A'

Extend the three if statement, to include two, so if two > 'Z', the one
wheel, will be incremented, and two and three reset to 'A'

and loop back

if one equals '@' then one = 32
if two equals '@') then two = 32

print num was: %d,  which is: %c%c%c num, one, two, three.```

If you don't have an ascii table for quick reference, grab one free from Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

When you're programming, you'll refer to it frequently.

Thanks for your help. I was able to complete this. Your logic helped me to write this program.
I never thought that this logic can be this much simple.

5. If you're a computer engineering guy, this is just a change of base from base 26 to base 10

6. You're quite welcome Lokachari, and BdON003 is right on the change of base.

7. Sorry I am not a computer science guy. I have no idea on how to do that using base26. I tried a program but is not working :
------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{

int i, number, user_input, index, remainder, digit[10];
char final[10], tmp[10];
int base26 = 26;
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

printf("Enter the number: (0 to quit)\n");
scanf("%d", &user_input);

number = user_input;

index = 0;
while(number != 0)
{
remainder = number % base26;
digit[index] = remainder;
printf("digit[%d] = %d\n", index, remainder);
number = number / base26;
index++;
}

printf("index = %d\n", index);
strcpy(final, "");
for (i = (index - 1); i >= 0; i--)
{
printf("i = %d\n", i);
//if (digit[i] == 0) digit[i] = 26;
//if (i == 0) digit[i]++;
//if (digit[i] == 0) digit[i]++;
sprintf(tmp, "%c", (digit[i] + 64));
printf("tmp = %s\n", tmp);
strcat(final, tmp);
}

return;
}
----------------------------------------------

8. I played with that idea of mod and etc., myself. I didn't like it.

As you "pick" off the digits, one by one, you have to add that digit to the sum for that column, and see if it's greater than or == to the base number.

When it is >= the base number, you need to carry over the base (26), to the next column to it's left, by incrementing whatever is now in that column, by one. Also, subtract 26 from the column you were summing up, of course.

If you use the code I posted above, you'll have the program done in about 10 lines of code, with simple copy and paste (and slight editing) for the two and one column "wheels".

All in one compact while loop that even the riders on the short ... nevermind.