Excel Sheet Column Title

🏠 ⬅️ ➡️

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

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

Example 1:

Input: columnNumber = 1 Output: "A "

Example 2:

Input: columnNumber = 28 Output: "AB "

Example 3:

Input: columnNumber = 701 Output: "ZY "

Constraints:

  • 1 <= columnNumber <= 231 - 1

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: columnNumber = 1
    character(len=5) :: columnTitle

    call getColumnTitle(columnNumber, columnTitle)
    print '(A)', trim(columnTitle)

contains

    subroutine getColumnTitle(columnNumber, columnTitle)
        implicit none
        integer, intent(in) :: columnNumber
        character(len=*), intent(out) :: columnTitle

        character(len=1), dimension(:), allocatable :: letters
        integer :: i, n

        allocate(letters(columnNumber))

        do i = 1, size(letters)
            letters(i) = char(iachar('A') + mod(columnNumber - 1, 26))
            columnNumber = (columnNumber - 1) / 26
        end do

        n = size(letters)
        if (n > 0) then
            columnTitle = repeat(' ', n-1) // letters(n)
        else
            columnTitle = ''
        end if
    end subroutine getColumnTitle
end program main
Compiled
Executed
Correct
```
PROGRAM columnTitle
    IMPLICIT NONE
    INTEGER :: columnNumber
    CHARACTER(LEN=3) :: columnTitle

    ! Read input
    READ(*,*) columnNumber

    ! Calculate column title
    columnTitle = getColumnTitle(columnNumber)

    ! Print output
    WRITE(*,*) columnTitle

CONTAINS

    FUNCTION getColumnTitle(columnNumber) RESULT(columnTitle)
        IMPLICIT NONE
        INTEGER, INTENT(IN) :: columnNumber
        CHARACTER(LEN=3) :: columnTitle
        INTEGER :: remainder

        ! Initialize column title
        columnTitle = ""

        ! Loop through digits of column number
        DO WHILE (columnNumber > 0)
            ! Calculate remainder
            remainder = MOD(columnNumber, 26)

            ! Add digit to column title
            IF (remainder == 0) THEN
                columnTitle = "Z" // columnTitle
                columnNumber = columnNumber / 26 - 1
            ELSE
                columnTitle = CHAR(remainder + 64) // columnTitle
                columnNumber = columnNumber / 26
            END IF
        END DO
    END FUNCTION getColumnTitle
END PROGRAM columnTitle
```
🌐 Data from online sources
def convert_to_title(columnNumber: int) -> str:
    result = ""
    while columnNumber:
        columnNumber -= 1
        result = chr(columnNumber % 26 + ord('A')) + result
        columnNumber //= 26
    return result
The algorithm converts an integer `columnNumber` to its corresponding column title as it appears in an Excel sheet. It does so by treating the Excel sheet column headers as a Base-26 number system where the numbers are represented by the characters from A-Z.
  1. We start by initializing an empty string called result that will store the final column title.
  2. In a while loop, we continue until the columnNumber is greater than 0.
  3. We subtract 1 from columnNumber, as the base representation starts from A (1).
  4. The remainder of columnNumber divided by 26 is the index of the character in the A-Z range, which is added to the result string.
  5. columnNumber is then divided by 26 and the loop continues until columnNumber is 0.
  6. Once the loop is finished, the result string is returned as the corresponding column title.
🌐 Data from online sources
#include <string>

std::string convertToTitle(int columnNumber) {
    std::string result = "";
    while (columnNumber) {
        columnNumber--;
        result = (char)(columnNumber % 26 + 'A') + result;
        columnNumber /= 26;
    }
    return result;
}
The algorithm converts an integer `columnNumber` to its corresponding column title as it appears in an Excel sheet. It does so by treating the Excel sheet column headers as a Base-26 number system where the numbers are represented by the characters from A-Z.
  1. We start by initializing an empty string called result that will store the final column title.
  2. In a while loop, we continue until the columnNumber is greater than 0.
  3. We subtract 1 from columnNumber, as the base representation starts from A (1).
  4. The remainder of columnNumber divided by 26 is the index of the character in the A-Z range, which is added to the result string.
  5. columnNumber is then divided by 26 and the loop continues until columnNumber is 0.
  6. Once the loop is finished, the result string is returned as the corresponding column title.