Java Conversion - Java Program to convert a given number to words



Java Conversion :

  • To convert a given number into words. For example, if “1234” is given as input, the output should be “one two three four”.

Sample Code

import java.util.Scanner;

public class conversion 
{
  public static void main(String[] args) 
  {
    int n, sum = 0, r;
    System.out.println("Enter the Number= ");
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    while (n > 0) 
    {
      r = n % 10;
      sum = sum * 10 + r;
      n = n / 10;
    }
    n = sum;
    while (n > 0) {
      r = n % 10;
      switch (r) {
      case 1:
        System.out.print("One ");
        break;
      case 2:
        System.out.print("Two ");
        break;
      case 3:
        System.out.print("Three ");
        break;
      case 4:
        System.out.print("Four ");
        break;
      case 5:
        System.out.print("Five ");
        break;
      case 6:
        System.out.print("Six ");
        break;
      case 7:
        System.out.print("Seven ");
        break;
      case 8:
        System.out.print("Eight ");
        break;
      case 9:
        System.out.print("Nine ");
        break;
      case 0:
        System.out.print("Zero ");
        break;
      }
      n = n / 10;
    }
  }
}

Output

Enter the Number = 12345
One Two Three Four Five

Related Searches to Java Conversion - Java Program to convert a given number to words