Java Program to convert a number into characters


In this program, You will learn how to convert a number into characters in java.


Number is: 3094

Three Zero Nine Four

Example: How to convert a number into characters in java.

import java.util.Scanner;

class Main {

    public static void main(String args[]) {

        int r, n, rev = 0;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a Number:");
        n = sc.nextInt();

        while (n > 0) {
            r = n % 10;
            rev = rev * 10 + r;
            n = n / 10;
        }
        n = rev;

        System.out.print("\n");

        while (n > 0) {
            r = n % 10;
            switch (r) {

                case 0:
                    System.out.print("Zero ");
                    break;

                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;
            }
            n = n / 10;
        }

    }
}

Output:

Enter a Number:34507

Three Four Five Zero Seven

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.