Source Code:: Question: Write A Program To Print Numbers in The Range 1-999 in Words. It Should Also Display

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Question: Write a program to print numbers in the range 1-999 in words.

It should also display


appropriate message if the input is outside the range.

Answer:

Source Code:
import java.util.*;

public class Number_to_word

void print_in_words(int n,String ch)

String one[]={" "," one"," two"," three"," four"," five"," six"," seven"," eight"," Nine"," ten","
eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};

String ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};

if(n > 19) { System.out.print(ten[n/10]+" "+one[n%10]);} else { System.out.print(one[n]);}

if(n > 0)System.out.print(ch);

public static void main()

int n=0;

Scanner scanf = new Scanner(System.in);

System.out.println("Enter an integer number: ");

n = scanf.nextInt();

if(n <= 0 || n>999)

{
System.out.println("Enter numbers in the range 1-999");

else

Number_to_word a = new Number_to_word();

a.print_in_words((n/1000000000)," Hundred");

a.print_in_words(((n/1000)%100)," thousand");

a.print_in_words(((n/100)%10)," hundred");

a.print_in_words((n%100)," ");

Screenshots:

You might also like