×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Common Elements In Array In Java - ICSE Examples Accept a name and print only the initials with surname.
WAP in java to accept a number (0 -999) and print it in words. - Java
12169    Arnab De    08/06/2018   

Write a programme in java to accept a number (0 -999) and print it in words.

This programme can wite in two way. in the first way is for begineers, simple convert the digits into words and print it. The Second way is little more complex, here we convert the numbers into words properly. and according to me This is the correct way for this program.

First way of Programming

import java.util.*;
public class NumberToWords
{
    public static void main(String []args)
    {
        String []nos={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter The Number : ");
        int no=sc.nextInt();
        int r;
        String res="";
        while(no>0)
        {
            r=no%10;
            res = nos[r] + " " + res;
            no /= 10;
        }
        System.out.print("\n " + res);
    }
}
softetechnologies

Output

Enter The Number : 756

Seven Five Six

 

Second way of Programming

 

import java.util.*;
public class NumberToWords
{
    public static void main(String []args)
    {
        String []nos={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter The Number : ");
        String no=sc.next();
        String res="";
        int no1=0,no2=0,no3=0;
        if(no.length()==3)
        {
            no1=Integer.parseInt(no.substring(0,1));
            no2=Integer.parseInt(no.substring(1,2));
            no3=Integer.parseInt(no.substring(2,3));
            res = nos[no1] + " Hundred " + nos[18+no2] + " ";
            if(no3>0)
                res += nos[no3];
            res += " Only";
        }
        
        System.out.print("\n  " + res);
    }
}
softetechnologies

Output

Enter The Number : 345
Three Hundred Fourty Five Only

 

Enter The Number : 670
Six Hundred Seventy  Only

Common Elements In Array In Java - ICSE Examples Accept a name and print only the initials with surname.
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Rabindranath Tagore
It is no easy task to lead men. But it is easy enough to drive them.
Rabindranath Tagore
1792
25.81
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     31582
28/06/2017     26609
02/08/2017     23328
26/05/2018     21522
15/05/2017     20221
06/07/2017     19001
01/08/2017     17462
21/04/2018     17234
01/07/2017     14866
25/04/2018     14109
softetechnologies