Using conditional if else statement print number in words

Below java code will print the integer value between 1 to 9 into words, any number other than 1 to 9 will be displayed as "OTHER".

import java.util.Scanner;

public class PrintNumberInWord {
    public static void main(String[] args) {
        System.out.println("Enter any integer number ");
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();

        if (number == 1)
            System.out.println("ONE");
        else if (number == 2)
            System.out.println("TWO");
        else if (number == 3)
            System.out.println("THREE");
        else if (number == 4)
            System.out.println("FOUR");
        else if (number == 5)
            System.out.println("FIVE");
        else if (number == 6)
            System.out.println("SIX");
        else if (number == 7)
            System.out.println("SEVEN");
        else if (number == 8)
            System.out.println("EIGHT");
        else if (number == 9)
            System.out.println("NINE");
        else
            System.out.println("OTHER");
    }
}


Output:

Enter any integer number
9
NINE

2 comments:

  1. Nice site....Please refer this site also Our vision success!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Real time methodologies
    Best Dot Net Training in Chennai
    Selenium Training in chennai
    Software Testing Coaching in Chennai
    Core Java Training in Chennai
    PHP Course in Chennai

    ReplyDelete
  2. import java.util.*;
    class method
    {
    public static void main()
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter a number between 1 and 1000");
    int n = sc.nextInt();
    String str[] = {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen","twenty"};
    String str1[] = {"","","","thirty","forty","fifty","sixty","seventy","eighty","ninty"};
    String str2[] = {"","hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred"};
    if(n<21)
    System.out.println(str[n]);
    else if(n<100)
    System.out.println(str1[n/10]+str[n%10]);
    else if(n<1000)
    System.out.println(str2[n/100]+str1[n%100]+str[n%10]);
    else
    System.out.println("one thousand");
    }
    }

    ReplyDelete