MS Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA”.
Given a column number, find its corresponding Excel column name. Following are more examples.
Input Output 26 Z 51 AY 52 AZ 80 CB 676 YZ 702 ZZ 705 AAC
Thanks to Mrigank Dembla for suggesting the below solution in a comment.
Suppose we have a number n, let’s say 28. so corresponding to it we need to print the column name. We need to take remainder with 26.
If remainder with 26 comes out to be 0 (meaning 26, 52 and so on) then we put ‘Z’ in the output string and new n becomes n/26 -1 because here we are considering 26 to be ‘Z’ while in actual it’s 25th with respect to ‘A’.
Similarly if the remainder comes out to be non zero. (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string and do n = n/26.
Finally we reverse the string and print.
n = 700
Remainder (n%26) is 24. So we put ‘X’ in output string and n becomes n/26 which is 26.
Remainder (26%26) is 0. So we put ‘Z’ in output string and n becomes n/26 -1 which is 0.
Following is C++ implementation of above approach.
Z AY AZ CB YZ ZZ AAC
Related Article :
Find Excel column number from column title
This article is contributed by Kartik. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Find Excel column number from column title
- Python | Plotting column charts in excel sheet using XlsxWriter module
- Program to find the Sum of each Row and each Column of a Matrix
- Find column with maximum sum in a Matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Find pair with maximum difference in any column of a Matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Count number of islands where every island is row-wise and column-wise separated
- Number of positions with Same address in row major and column major order
- Allow only alphabets in column in SQL Server
- Sum of middle row and column in Matrix
- Sorting 2D Vector in C++ | Set 1 (By row and column)
- Check if sums of i-th row and i-th column are same in matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Replace every matrix element with maximum of GCD of row or column