C program to print Excel column titles based on given column number

In C programming, converting a column number to its corresponding Excel column title is a common problem. Excel columns are labeled as A, B, C, ..., Z, AA, AB, AC, and so on. This follows a base-26 numbering system where A=1, B=2, ..., Z=26, AA=27, etc.

Syntax

char* convertToExcelTitle(int columnNumber);

Algorithm

The algorithm works by repeatedly dividing the column number by 26 and converting the remainder to the corresponding character −

  • Subtract 1 from the column number to make it 0-indexed
  • Find the remainder when divided by 26 and convert to character
  • Divide by 26 and repeat until the number becomes 0
  • Reverse the resulting string to get the final title

Example

Following is a complete C program to convert column numbers to Excel column titles −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* convertToExcelTitle(int columnNumber) {
    if (columnNumber <= 0) {
        char* empty = malloc(1);
        empty[0] = '\0';
        return empty;
    }
    
    char* result = malloc(10); // Sufficient for most Excel columns
    int len = 0;
    
    while (columnNumber > 0) {
        columnNumber--; // Make it 0-indexed
        result[len++] = (columnNumber % 26) + 'A';
        columnNumber = columnNumber / 26;
    }
    
    result[len] = '\0';
    
    // Reverse the string
    for (int i = 0; i < len / 2; i++) {
        char temp = result[i];
        result[i] = result[len - 1 - i];
        result[len - 1 - i] = temp;
    }
    
    return result;
}

int main() {
    int testCases[] = {1, 26, 27, 28, 52, 701, 702};
    int numTests = sizeof(testCases) / sizeof(testCases[0]);
    
    printf("Excel Column Number to Title Conversion:<br>");
    printf("----------------------------------------<br>");
    
    for (int i = 0; i < numTests; i++) {
        char* title = convertToExcelTitle(testCases[i]);
        printf("Column %d -> %s<br>", testCases[i], title);
        free(title);
    }
    
    return 0;
}
Excel Column Number to Title Conversion:
----------------------------------------
Column 1 -> A
Column 26 -> Z
Column 27 -> AA
Column 28 -> AB
Column 52 -> AZ
Column 701 -> ZY
Column 702 -> ZZ

How It Works

The conversion process follows these steps −

  • Step 1: Decrement the column number to make it 0-indexed (since A=1, but we want A=0 for modulo operation)
  • Step 2: Take modulo 26 to get the rightmost character
  • Step 3: Divide by 26 to move to the next position
  • Step 4: Repeat until the number becomes 0
  • Step 5: Reverse the resulting string to get the correct order

Key Points

  • Excel uses a bijective base-26 system where there's no "zero" digit
  • Memory allocation is required for the result string
  • Always free the allocated memory to prevent memory leaks
  • The algorithm has O(log??n) time complexity

Conclusion

Converting column numbers to Excel titles requires understanding bijective base-26 numeration. The key insight is decrementing before the modulo operation to handle the absence of zero in this numbering system.

Updated on: 2026-03-15T14:20:18+05:30

873 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements