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

## Problem

A program to print Excel column title that corresponds to a given column number (integer value). User has to enter the integer number based on given number it has to print excel column number.

## Solution

The solution to print Excel column title that corresponds to a given column number in C programming language is explained below −

Example 1

Let us see an example.

1 -> A
2 -> B
...
26 -> Z
27 -> AA
28 -> AB
...

Example 2

• The input is as follows −
number = 3
number = 27
number = 151
• The output is as follows −
Excel column title: C
Excel column title: AA
Excel column title: EU

## Example

Following is the C program to print the Excel column title that corresponds to a given column number

Live Demo

#include <stdio.h>
static char *convert_To_Excel_Title(int column_no){
if (column_no <= 0) {
return "";
}
char *result = malloc(1024);
int len = 0;
do {
result[len++] = ((column_no - 1) % 26) + 'A';
column_no = (column_no - 1) / 26;
} while (column_no > 0);
result[len] = '\0';
int i, j;
for (i = 0, j = len - 1; i < j; i++, j--) {
char c = result[i];
result[i] = result[j];
result[j] = c;
}
return result;
}
int main(void){
int n;
printf("enter the integer1:");
scanf("%d",&n);
printf("Column Number n = %d", n);
printf("Excel column title: %s ",convert_To_Excel_Title(n));
printf("enter the integer2:");
scanf("%d",&n);
printf("Column Number n = %d", n);
printf("Excel column title: %s ",convert_To_Excel_Title(n));
printf("enter the integer3:");
scanf("%d",&n);
printf("Column Number n = %d", n);
printf("Excel column title: %s ",convert_To_Excel_Title(n));
return 0;
}

## Output

When the above program is executed, it produces the following result −

enter the integer1:23
Column Number n = 23
Excel column title: W

enter the integer2:12

Column Number n = 12
Excel column title: L

enter the integer3:69

Column Number n = 69
Excel column title: BQ

Updated on: 03-Sep-2021

573 Views