Convert number into corresponding excel column name

Given a positive number, convert the number into corresponding excel column name.


 

For example, below image shows numbers corresponding to excel columns.
 

excel column number to name

The main trick in the problem lies in the handling the boundary cases like number 26 corresponds to ‘Z’ and 27 corresponds to ‘AA’. Similarly, number 1014 corresponds to ‘ALZ’ and 1015 corresponds to ‘AMA’. Below is the short piece of code that handle all these cases beautifully.

 
Below is C++ and Java implementation of the idea –

C++

Download   Run Code

Output:

585 – VM
873 – AGO
269 – JI
849 – AFQ
288 – KB
962 – AJZ
549 – UC
572 – UZ
485 – RQ
704 – AAB

Java

Download   Run Code

Output:

527 – TG
562 – UP
876 – AGR
167 – FK
499 – SE
90 – CL
997 – ALI
592 – VT
758 – ACD
739 – ABK

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂


Leave a Reply

avatar
  Subscribe  
Notify of