Comment on Tutorial - Converting a number into its equalant value in words in Java By Ganesh Iyer
Comment Added by : Abhishek
Comment Added at : 2011-09-05 10:42:20
Comment on Tutorial : Converting a number into its equalant value in words in Java By Ganesh Iyer
//Convert numbers to word as per Indian number system in java by if loop. It works up to 999999999.
import java.util.*;
public class NumtoWord
{
public static void main(String[] args)
{
String a;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number : ");
a = s.next();
int b = a.length();
String str[] = {"1","One","2","Two","3","Three","4","Four","5","Five","6","Six","7","Seven","8","Eight","9","Nine","10","Ten","11","Eleven","12","Twelve","13","Thirteen","14","Forteen","15","Fifteen","16","Sixteen","17","Seventeen","18","Eighteen","19","Nineteen","20","Twenty","30","Thirty","40","Fourty","50","Fifty","60","Sixty","70","Seventy","80","Eighty","90","Ninty","100","Hundred"};
System.out.println("");
if (b==9)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s7= a.substring(6,7);
String s8= a.substring(7,8);
String s9= a.substring(8,9);
String s10= a.substring(0,2);
String s11= a.substring(2,4);
String s12= a.substring(4,6);
String s14= a.substring(7,9);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print("\n" + str[r+1] + " Crore ");
}
else
{
{
for (int i=0;i<=40;i++)
if (str[i].equals(s1))
System.out.print("\n" + str[i+37] + " ");
}
{
if(s2.equals("0"))
{
System.out.print("Crore ");
}
else
for (int j=0;j<=40;j++)
{
if (str[j].equals(s2))
System.out.print(str[j+1] + " Crore ");
}
}
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s3.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11))
System.out.print(str[r+1] + " Lacks ");
}
else
{
{
for (int k=0;k<=38;k++)
if (str[k].equals(s3))
System.out.print(str[k+37] + " ");
}
{
if(s4.equals("0"))
{
System.out.print("Lacks ");
}
else
for (int l=0;l<=38;l++)
{
if (str[l].equals(s4))
System.out.print(str[l+1] + " Lacks ");
}
}
}
}
{
if (s12.equals("00"))
System.out.print("");
else if (s5.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s12))
System.out.print(str[r+1] + " Thousand ");
}
else
{
{
for (int m=0;m<=38;m++)
if (str[m].equals(s5))
System.out.print(str[m+37] + " ");
}
{
if(s6.equals("0"))
{
System.out.print("Thousand ");
}
else
for (int n=0;n<=38;n++)
{
if (str[n].equals(s6))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s7))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s14.equals("00"))
System.out.print("");
else if (s8.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s14))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s8))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s9))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==8)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s7= a.substring(6,7);
String s8= a.substring(7,8);
String s10= a.substring(1,3);
String s11= a.substring(3,5);
String s12= a.substring(6,8);
{
if (s1.equals("0"))
System.out.print("");
else
for (int i=0;i<=40;i++)
if (str[i].equals(s1))
System.out.print("\n" + str[i+1] + " Crore ");
}
{
if (s10.equals("00"))
System.out.print("");
else if (s2.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1] + " Lacks ");
}
else
{
{
for (int k=0;k<=38;k++)
if (str[k].equals(s2))
System.out.print(str[k+37] + " ");
}
{
if(s3.equals("0"))
{
System.out.print("Lacks ");
}
else
for (int l=0;l<=38;l++)
{
if (str[l].equals(s3))
System.out.print(str[l+1] + " Lacks ");
}
}
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s4.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11))
System.out.print(str[r+1] + " Thousand ");
}
else
{
{
for (int m=0;m<=38;m++)
if (str[m].equals(s4))
System.out.print(str[m+37] + " ");
}
{
if(s5.equals("0"))
{
System.out.print("Thousand ");
}
else
for (int n=0;n<=38;n++)
{
if (str[n].equals(s5))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s6))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s12.equals("00"))
System.out.print("");
else if (s7.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s12))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s7))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s8))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==7)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s7= a.substring(6,7);
String s10= a.substring(0,2);
String s11= a.substring(2,4);
String s12= a.substring(5,7);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1] + " Lacks ");
}
else
{
{
for (int k=0;k<=38;k++)
if (str[k].equals(s1))
System.out.print(str[k+37] + " ");
}
{
if(s2.equals("0"))
{
System.out.print("Lacks ");
}
else
for (int l=0;l<=38;l++)
{
if (str[l].equals(s2))
System.out.print(str[l+1] + " Lacks ");
}
}
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s3.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11))
System.out.print(str[r+1] + " Thousand ");
}
else
{
{
for (int m=0;m<=38;m++)
if (str[m].equals(s3))
System.out.print(str[m+37] + " ");
}
{
if(s4.equals("0"))
{
System.out.print("Thousand ");
}
else
for (int n=0;n<=38;n++)
{
if (str[n].equals(s4))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s5))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s12.equals("00"))
System.out.print("");
else if (s6.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s12))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s6))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s7))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==6)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s10= a.substring(1,3);
String s11= a.substring(4,6);
{
if(s1.equals("0"))
System.out.print("");
else
{
for (int j=0;j<=40;j++)
if (str[j].equals(s1))
System.out.print(str[j+1] + " Lacks ");
}
}
{
if (s10.equals("00"))
System.out.print("");
else if (s2.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1] + " Thousand ");
}
else
{
{
for (int m=0;m<=40;m++)
if (str[m].equals(s2))
System.out.print(str[m+37] + " ");
}
{
if(s3.equals("0"))
{
System.out.print("Thousand ");
}
else
for (int n=0;n<=38;n++)
{
if (str[n].equals(s3))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
if(s4.equals("0"))
System.out.print("");
else
{
for (int o=0;o<=40;o++)
if (str[o].equals(s4))
System.out.print(str[o+1] + " Hundred ");
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s5.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s5))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s6))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==5)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s10= a.substring(0,2);
String s11= a.substring(3,5);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1] + " Thousand ");
}
else
{
{
for (int m=0;m<=38;m++)
if (str[m].equals(s1))
System.out.print(str[m+37] + " ");
}
{
if(s2.equals("0"))
{
System.out.print("Thousand ");
}
else
for (int n=0;n<=38;n++)
{
if (str[n].equals(s2))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s3))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s11.equals("00"))
System.out.print("");
else if (s4.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s4))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s5))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==4)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s10= a.substring(2,4);
{
if(s1.equals("0"))
System.out.print("");
else
{
for (int j=0;j<=40;j++)
if (str[j].equals(s1))
System.out.print(str[j+1] + " Thousand ");
}
}
{
if(s2.equals("0"))
System.out.print("");
else
{
for (int o=0;o<=40;o++)
if (str[o].equals(s2))
System.out.print(str[o+1] + " Hundred ");
}
}
{
if (s10.equals("00"))
System.out.print("");
else if (s3.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s3))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s4))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==3)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s10= a.substring(1,3);
{
if(s1.equals("0"))
System.out.print("");
else
{
for (int o=0;o<=40;o++)
if (str[o].equals(s1))
System.out.print(str[o+1] + " Hundred ");
}
}
{
if (s10.equals("00"))
System.out.print("");
else if (s2.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s2))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s3))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==2)
{
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s10= a.substring(0,2);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10))
System.out.print(str[r+1]);
System.out.print("\n");
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s1))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s2))
System.out.print(" " + str[q+1]);
}
}
System.out.print("\n");
}
}
else if (b==1)
{
String s1= a.substring(0,1);
for (int q=0;q<=40;q++)
if (str[q].equals(s1))
System.out.print(" " + str[q+1]);
}
System.out.println("\n");
}
}
// By Abhishek Singh ([email protected]).
View Tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
Archived Comments
1. i want to SCJP exam ,tell how to apply for this ex
View Tutorial By: jasmina at 2013-04-22 11:05:53
2. Nice example..i m working on android and i want to
View Tutorial By: Pranav at 2012-04-25 07:00:48
3. Could you please send me steps to execute the code
View Tutorial By: Roshan at 2009-12-23 21:32:29
4. how can i give me a assign value to mutldimentiona
View Tutorial By: shinde datta at 2013-01-12 10:41:15
5. my quseation is why use we return statement
View Tutorial By: pankaj kumar at 2010-08-31 03:53:48
6. Everything is running.... But it doesn't send....
View Tutorial By: Amirtharaj at 2009-04-14 05:26:26
7. Just read the first page, It really attractive. Le
View Tutorial By: Md. Sumon Bappi at 2015-05-13 10:47:00
8. Thanks for the explanation.
It makes sense
View Tutorial By: Miguel JImenez at 2009-05-10 14:28:46
9. I have loaded allthe 5 java files.But how to imple
View Tutorial By: Pandiyan at 2015-08-24 08:02:32
10. help me pls example ng c++ program code using do
View Tutorial By: diana bernardo at 2010-10-15 23:34:19