Open In App

Excel column name from a given column number

Last Updated : 27 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Find the remainder when dividing the column number by 26 to determine the current letter.
  2. If the remainder is 0, append ‘Z’ and adjust the number by subtracting 1 from the integer division result.
  3. If the remainder is non-zero, convert it to the corresponding letter by subtracting 1 and adding ‘A’.
  4. Continuously divide the number by 26 to move to the next digit.
  5. Reverse the generated string to get the correct column name.

Example Illustration:

Taking example of n = 3588

  1. n = 3588, n % 26 = 0
    • 0 corresponds to ‘Z’ . Resultant string will be “Z”.
    • Divide n by 26 and subtract 1 from it.
  2. n = 137, n % 26 = 7
    • 7 corresponds to ‘G’. Resultant string will be “ZG”.
    • Divide n by 26.
  3. n = 5, n % 26 = 5
    • 5 corresponds to ‘E’. Resultant string will be “ZGE”.
    • Divide n by 26.
  4. n = 0, stop the process.
  5. As string was generated from right to left, so reverse the string to get the correct column name.
    • String will become “EGZ”

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));

Output
ZZ

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:

  1. Establish a base case that returns an empty string when the number becomes zero.
  2. Recursively divide the number by 26 to break it down to its smallest components.
  3. Decrement the number before calculating the remainder to handle the 1-indexing.
  4. Convert the remainder to its corresponding alphabetic character by adding it to ‘A’.
  5. 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));

Output
ZZ

Related Article : 
Find the Excel column number from the column title



Similar Reads