w3resource

Java: Find the Excel column name

Java Math Exercises: Exercise-29 with Solution

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

Flowchart:

Flowchart: Find the Excel column name.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/math/java-math-exercise-29.php