w3resource

Java: Find the Excel column name

Java Math Exercises: Exercise-29 with Solution

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.



Follow us on Facebook and Twitter for latest update.