Java program to print a number in word [in 2 ways] - codeyourslf

In this article you can learn how to print a number in it's corresponding words in java with 2 different approaches. Codeyourslf - a programming blog
5 min read

print a number in word [in 2 ways] - codeyourslf

Java program to print a number in word [in 2 ways] - codeyourslf

In this article you can learn how to print a number in it's corresponding words in java with 2 different approaches.

  • Ex :- Input - 3621
  •         Output - Three Six Two One

Here I also use my android phone to write code. I use android java ide i.e java N-IDE. Please support.

Approach:-1 "java program to print number in words using switch case"

public class usingSwitchCase {

  public static void main(String[] args) {

    int num = 3725;

    int temp = num;

    int revNum = 0;

    while(num > 0){

      revNum = revNum * 10 + (num % 10);

      num = num /10;

    }

    System.out.println(temp);

    printWord(revNum);

  }

  public static void printWord(int revNum){

    while(revNum > 0){

      int digit = revNum % 10;

      switch(digit){

        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;

      }

      revNum = revNum / 10;

    }

  }

}

     
+
Explanation

We can't access an integer from index 0 to last, but using modulus operator(%) we can access an integer in from last index to first. 

So, first I reverse the given number and store it in revNum variable. 

Now in switch case again I start accessing the number from last index, as a result the order of the number become same. 

Inside switch case, I assigned cases from 0 to 9. Here if the digit of a number is 3 then it will print Three.

Output

java program to print number in words using switch case

Approach:-2 "print digit of a number in words using String concept"

public class usingString {

    public static void main(String[] args){

      String num_s = "357";

      int len = num_s.length();

      System.out.println("using string concept: "+num_s);

      for(int i = 0; i < len; i++){

        printWords(num_s.charAt(i));

      }

    }

    

    public static void printWords(char c){

      switch(c){

        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;

      }

    }

}

    

Output

print digit of a number in words using String concept

Hey if you learn something new from this article please share with your friends.

And if you have any questions or any doubt please ask me in the comment section, I will definitely answer to your questions.

`

You may like these posts

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍