Excel column name from a given column number
Last Updated : 23 Jul, 2025
MS Excel columns have a pattern like A, B, C, …, Z, AA, AB, AC, …., AZ, BA, BB, … ZZ, AAA, AAB ..... etc. In other words, column 1 is named "A", column 2 as "B", and column 27 as "AA".
Given a column number, the task is to find its corresponding Excel column name.
Examples:
Input: 26
Output: Z
Input: 51
Output: AY
Input: 676
Output: YZ
Input: 705
Output: AAC
Input: 80
Output: CB
Approach 1 - Using Iterative Method - O(Log26 n) time and O(1) space
The idea is to craft a base-26 conversion process that maps numbers to letters, mimicking how we count but with an alphabetic twist. The algorithm works by repeatedly dividing the number and using the remainders to generate the column name, with special handling for the case when the remainder is zero (which represents 'Z'). This requires working backwards from the least significant digit and then reversing the final string to get the correct column name.
Step by step approach:
- Find the remainder when dividing the column number by 26 to determine the current letter.
- If the remainder is 0, append 'Z' and adjust the number by subtracting 1 from the integer division result.
- If the remainder is non-zero, convert it to the corresponding letter by subtracting 1 and adding 'A'.
- Continuously divide the number by 26 to move to the next digit.
- Reverse the generated string to get the correct column name.
Example Illustration:
Taking example of n = 3588
- n = 3588, n % 26 = 0
- 0 corresponds to 'Z' . Resultant string will be "Z".
- Divide n by 26 and subtract 1 from it.
- n = 137, n % 26 = 7
- 7 corresponds to 'G'. Resultant string will be "ZG".
- Divide n by 26.
- n = 5, n % 26 = 5
- 5 corresponds to 'E'. Resultant string will be "ZGE".
- Divide n by 26.
- n = 0, stop the process.
- As string was generated from right to left, so reverse the string to get the correct column name.
Why 1 must be subtracted from n when 'Z' is encountered?
The subtraction of 1 when encountering a remainder of 0 (which maps to 'Z') is crucial because Excel's column naming system is essentially a 1-indexed, base-26 numbering scheme. Unlike typical base conversions where you might start at 0, Excel columns start at 1 ('A'). This means that when you hit a multiple of 26 (like 26, 52, 78), the numbering requires a special adjustment. By subtracting 1 during the division process, the algorithm ensures that these multiples correctly map to 'Z', 'AZ', 'BZ', and so on, maintaining the precise one-to-one mapping between column numbers and their alphabetic representations. Without this subtraction, the conversion would be off by one, leading to incorrect column names.
C++ // C++ program to Find Excel column // name from a given column number #include <bits/stdc++.h> using namespace std; string colName(int n) { string res = ""; while (n > 0) { // Find remainder int rem = n % 26; // If remainder is 0, then a 'Z' // must be there in output if (rem == 0) { res += 'Z'; n = (n / 26) - 1; } // If remainder is non-zero else { res += (rem - 1) + 'A'; n = n / 26; } } // Reverse the string reverse(res.begin(), res.end()); return res; } int main() { int n = 702; cout << colName(n); return 0; }
Java // Java program to Find Excel column // name from a given column number import java.util.*; class GfG { static String colName(int n) { StringBuilder res = new StringBuilder(); while (n > 0) { // Find remainder int rem = n % 26; // If remainder is 0, then a 'Z' // must be there in output if (rem == 0) { res.append('Z'); n = (n / 26) - 1; } // If remainder is non-zero else { res.append((char) ((rem - 1) + 'A')); n = n / 26; } } // Reverse the string res.reverse(); return res.toString(); } public static void main(String[] args) { int n = 702; System.out.println(colName(n)); } }
Python # Python program to Find Excel column # name from a given column number def colName(n): res = "" while n > 0: # Find remainder rem = n % 26 # If remainder is 0, then a 'Z' # must be there in output if rem == 0: res += 'Z' n = (n // 26) - 1 # If remainder is non-zero else: res += chr((rem - 1) + ord('A')) n //= 26 # Reverse the string return res[::-1] if __name__ == "__main__": n = 702 print(colName(n))
C# // C# program to Find Excel column // name from a given column number using System; class GfG { static string colName(int n) { string res = ""; while (n > 0) { // Find remainder int rem = n % 26; // If remainder is 0, then a 'Z' // must be there in output if (rem == 0) { res += 'Z'; n = (n / 26) - 1; } // If remainder is non-zero else { res += (char)((rem - 1) + 'A'); n /= 26; } } // Reverse the string char[] charArray = res.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } static void Main() { int n = 702; Console.WriteLine(colName(n)); } }
JavaScript // JavaScript program to Find Excel column // name from a given column number function colName(n) { let res = ""; while (n > 0) { // Find remainder let rem = n % 26; // If remainder is 0, then a 'Z' // must be there in output if (rem === 0) { res += 'Z'; n = Math.floor(n / 26) - 1; } // If remainder is non-zero else { res += String.fromCharCode((rem - 1) + 'A'.charCodeAt(0)); n = Math.floor(n / 26); } } // Reverse the string return res.split("").reverse().join(""); } let n = 702; console.log(colName(n));
Time Complexity: O(log26n), as we are using a loop and in each traversal, we decrement by floor division of 26.
Auxiliary Space: O(1)
Approach 2 - Using Recursion - O(Log26 n) time and O(Log26 n) space
The idea is to recursively break down the column number into its constituent parts by continuously dividing by 26 and generating letters from the remainders. The recursive approach handles the base case by returning an empty string when the number becomes zero, and builds the column name from right to left by progressively transforming remainders into corresponding alphabetic characters.
Step by step approach:
- Establish a base case that returns an empty string when the number becomes zero.
- Recursively divide the number by 26 to break it down to its smallest components.
- Decrement the number before calculating the remainder to handle the 1-indexing.
- Convert the remainder to its corresponding alphabetic character by adding it to 'A'.
- Concatenate the recursive call result with the generated character to build the column name.
C++ // C++ program to Find Excel column // name from a given column number #include <bits/stdc++.h> using namespace std; string colName(int n) { // base case if (n == 0) return ""; // decrement n to handle 1 based indexing n--; return colName(n / 26) + (char) (n % 26 + 'A'); } int main() { int n = 702; cout << colName(n); return 0; }
Java // Java program to Find Excel column // name from a given column number class GfG { static String colName(int n) { // base case if (n == 0) return ""; // decrement n to handle 1 based indexing n--; return colName(n / 26) + (char) (n % 26 + 'A'); } public static void main(String[] args) { int n = 702; System.out.println(colName(n)); } }
Python # Python program to Find Excel column # name from a given column number def colName(n): # base case if n == 0: return "" # decrement n to handle 1 based indexing n -= 1 return colName(n // 26) + chr(n % 26 + ord('A')) if __name__ == "__main__": n = 702 print(colName(n))
C# // C# program to Find Excel column // name from a given column number using System; class GfG { static string colName(int n) { // base case if (n == 0) return ""; // decrement n to handle 1 based indexing n--; return colName(n / 26) + (char)(n % 26 + 'A'); } static void Main() { int n = 702; Console.WriteLine(colName(n)); } }
JavaScript // JavaScript program to Find Excel column // name from a given column number function colName(n) { // base case if (n === 0) return ""; // decrement n to handle 1 based indexing n--; return colName(Math.floor(n / 26)) + String.fromCharCode(n % 26 + 'A'.charCodeAt(0)); } let n = 702; console.log(colName(n));
Related Article :
Find the Excel column number from the column title
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem
My Profile