w3resource

Java: Find the Excel column name


Excel Column from Number

Write a Java program to find the Excel column name that corresponds to a given column number (integer value).

Sample Solution:

Java Code:

import java.util.*;
import java.lang.Character;

class solution {
  public static String Column(int n) {
    String str_column = "";

    while (n > 0) {
      int col_val = (n - 1) % 26;
      str_column = (char)(col_val + 'A') + str_column;
      n = (n - 1) / 26;
    }

    return str_column;
  }
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Input the number: ");
    int n = scan.nextInt();

    System.out.println("Excel Column: " + Column(n));
  }
}

Sample Output:

Input the number:  5
Excel Column: E

For more Practice: Solve these Related Problems:

  • Write a Java program to convert a column number to its corresponding Excel column name using recursion.
  • Write a Java program to handle conversion for numbers greater than 26 using iterative division and modulo arithmetic.
  • Write a Java program to convert a list of column numbers to Excel column names using Java streams.
  • Write a Java program to reverse the conversion by mapping an Excel column name back to its column number.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Multiply two numbers using bitwise operators.
Next: Find the angle between the hour and minute hands.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.