/*
*Before we get into it, this is strictly intended for beginners, i've organized the code in such a way that in *which even a beginner(might be a basic understanding about java) could understand . just look at code
* and see what happens. I've tried to make it quite simpler with comments. so, beginners, spend some time
* in this tutorial and develop it further for , say for 12 digits.
*Originally written by :sarathantony@yahoo.co.in
*/
package figuretowords;
import java.io.*;
/**
*
* @author sarathantony */
public class FigureToWords {
static int i=0;
//for 1,2,3,etc.... also further for <twenty>one, <thirty>two, etc..
//u get the idea?, even if u don't u'll get it when u reach main method
public void singleDigit(int i) {
switch(i) {
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;
case 0: System.out.print("");
break;
}
}
//for 10,20,30,etc...
public void twoDigit(int i) {
switch(i) {
case 1: System.out.print("ten");
break;
case 2: System.out.print("twenty");
break;
case 3: System.out.print("thirty");
break;
case 4: System.out.print("fourty");
break;
case 5: System.out.print("fifty");
break;
case 6: System.out.print("sixty");
break;
case 7: System.out.print("seventy");
break;
case 8: System.out.print("eighty");
break;
case 9: System.out.print("ninenty");
break;
case 0: System.out.print("");
break;
}
}
public void caseOne(int i) {
switch(i) {
case 1: System.out.print("eleven");
break;
case 2: System.out.print("twelve");
break;
case 3: System.out.print("thirteen");
break;
case 4: System.out.print("fourteen");
break;
case 5: System.out.print("fifteen");
break;
case 6: System.out.print("sixteen");
break;
case 7: System.out.print("seventeen");
break;
case 8: System.out.print("eighteen");
break;
case 9: System.out.print("ninenteen");
break;
case 0: System.out.print("");
break;
}
}
public static void main(String[] args) {
try {
FigureToWords ac= new FigureToWords();
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number:");
String number= br.readLine();
int length= number.length();
String[] str= number.split("");
int[] amount= new int[length];
for(i=0; i<length;i++) {
amount[i]= Integer.parseInt(str[i+1]);
}
// u get that idea, why i use a flag?
int flag;
for( i=0, flag=length ; i<length && flag>=1; ) {
switch(flag) {
case 9:
if(amount[i] == 1){
ac.caseOne(amount[i+1]);
System.out.print(" crore ");
i=i+2;
flag=flag-2;
break;
}
else if(amount[i+1]==0){
ac.twoDigit(amount[i]);
System.out.print(" crore ");
i=i+2;
flag=flag-2;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
System.out.print(" crore ");
break;
}
case 8:
ac.singleDigit(amount[i]);
System.out.print(" crore ");
i++;
flag--;
break;
case 7:
if(amount[i] == 1){
ac.caseOne(amount[i]);
i=i+2;
flag=flag-2;
System.out.print(" lakh ");
break;
}
else if(amount[i+1]==0){
ac.twoDigit(amount[i]);
i=i+2;
flag=flag-2;
System.out.print(" lakh ");
break;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
System.out.print("lakh");
break;
}
case 6:
ac.singleDigit(amount[i]);
System.out.print(" lakh ");
i++;
flag--;
break;
case 5:
if(amount[i] == 1){
ac.caseOne(amount[i]);
System.out.print(" thousand ");
i=i+2;
flag=flag-2;
break;
}
else if(amount[i+1]==0){
ac.twoDigit(amount[i]);
i=i+2;
flag=flag-2;
System.out.print(" thousand ");
break;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
System.out.print(" thousand ");
break;
}
case 4:
ac.singleDigit(amount[i]);
i++;
flag--;
System.out.print(" thousand ");
break;
case 3:
ac.singleDigit(amount[i]);
i++;
flag--;
System.out.print(" hunderd ");
break;
case 2:
if(amount[i] == 0) {
i=i+1;
flag=flag-1;
break;
}
else if (amount[i]==1) {
ac.caseOne(amount[i+1]);
i=i+2;
flag=flag-2;
break;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
break;
}
case 1:
ac.singleDigit(amount[i]);
i++;
flag--;
break;
default:
System.out.println("fialed");
}//end of switch
}//end of for
System.out.print("only");
System.out.println("");
}
catch (Exception e) {
System.out.println("ERROR"+e);
}
}//end of main
}//end of class
//well, this isn't the end, u can still expand it further or u could convert a float too..
//but remember, this is just a beginner's tutorial.
//for further, find me on facebook.com/sarathantony
*Before we get into it, this is strictly intended for beginners, i've organized the code in such a way that in *which even a beginner(might be a basic understanding about java) could understand . just look at code
* and see what happens. I've tried to make it quite simpler with comments. so, beginners, spend some time
* in this tutorial and develop it further for , say for 12 digits.
*Originally written by :sarathantony@yahoo.co.in
*/
package figuretowords;
import java.io.*;
/**
*
* @author sarathantony */
public class FigureToWords {
static int i=0;
//for 1,2,3,etc.... also further for <twenty>one, <thirty>two, etc..
//u get the idea?, even if u don't u'll get it when u reach main method
public void singleDigit(int i) {
switch(i) {
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;
case 0: System.out.print("");
break;
}
}
//for 10,20,30,etc...
public void twoDigit(int i) {
switch(i) {
case 1: System.out.print("ten");
break;
case 2: System.out.print("twenty");
break;
case 3: System.out.print("thirty");
break;
case 4: System.out.print("fourty");
break;
case 5: System.out.print("fifty");
break;
case 6: System.out.print("sixty");
break;
case 7: System.out.print("seventy");
break;
case 8: System.out.print("eighty");
break;
case 9: System.out.print("ninenty");
break;
case 0: System.out.print("");
break;
}
}
public void caseOne(int i) {
switch(i) {
case 1: System.out.print("eleven");
break;
case 2: System.out.print("twelve");
break;
case 3: System.out.print("thirteen");
break;
case 4: System.out.print("fourteen");
break;
case 5: System.out.print("fifteen");
break;
case 6: System.out.print("sixteen");
break;
case 7: System.out.print("seventeen");
break;
case 8: System.out.print("eighteen");
break;
case 9: System.out.print("ninenteen");
break;
case 0: System.out.print("");
break;
}
}
public static void main(String[] args) {
try {
FigureToWords ac= new FigureToWords();
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number:");
String number= br.readLine();
int length= number.length();
String[] str= number.split("");
int[] amount= new int[length];
for(i=0; i<length;i++) {
amount[i]= Integer.parseInt(str[i+1]);
}
// u get that idea, why i use a flag?
int flag;
for( i=0, flag=length ; i<length && flag>=1; ) {
switch(flag) {
case 9:
if(amount[i] == 1){
ac.caseOne(amount[i+1]);
System.out.print(" crore ");
i=i+2;
flag=flag-2;
break;
}
else if(amount[i+1]==0){
ac.twoDigit(amount[i]);
System.out.print(" crore ");
i=i+2;
flag=flag-2;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
System.out.print(" crore ");
break;
}
case 8:
ac.singleDigit(amount[i]);
System.out.print(" crore ");
i++;
flag--;
break;
case 7:
if(amount[i] == 1){
ac.caseOne(amount[i]);
i=i+2;
flag=flag-2;
System.out.print(" lakh ");
break;
}
else if(amount[i+1]==0){
ac.twoDigit(amount[i]);
i=i+2;
flag=flag-2;
System.out.print(" lakh ");
break;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
System.out.print("lakh");
break;
}
case 6:
ac.singleDigit(amount[i]);
System.out.print(" lakh ");
i++;
flag--;
break;
case 5:
if(amount[i] == 1){
ac.caseOne(amount[i]);
System.out.print(" thousand ");
i=i+2;
flag=flag-2;
break;
}
else if(amount[i+1]==0){
ac.twoDigit(amount[i]);
i=i+2;
flag=flag-2;
System.out.print(" thousand ");
break;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
System.out.print(" thousand ");
break;
}
case 4:
ac.singleDigit(amount[i]);
i++;
flag--;
System.out.print(" thousand ");
break;
case 3:
ac.singleDigit(amount[i]);
i++;
flag--;
System.out.print(" hunderd ");
break;
case 2:
if(amount[i] == 0) {
i=i+1;
flag=flag-1;
break;
}
else if (amount[i]==1) {
ac.caseOne(amount[i+1]);
i=i+2;
flag=flag-2;
break;
}
else {
ac.twoDigit(amount[i]);
ac.singleDigit(amount[i+1]);
i=i+2;
flag=flag-2;
break;
}
case 1:
ac.singleDigit(amount[i]);
i++;
flag--;
break;
default:
System.out.println("fialed");
}//end of switch
}//end of for
System.out.print("only");
System.out.println("");
}
catch (Exception e) {
System.out.println("ERROR"+e);
}
}//end of main
}//end of class
//well, this isn't the end, u can still expand it further or u could convert a float too..
//but remember, this is just a beginner's tutorial.
//for further, find me on facebook.com/sarathantony