Please open the menu to show more

Monday, August 21, 2017

Awesome code by MAYANK to convert Number to Word, Non Recursive approach

This code is implemented and provided by Mayank Banga (WW), 
from GBU, Greater Noida.

About this code : By using this code we can translate a number into a word without using recursion

For example 1 will become one
and 120 will become  1 hundred twenty 

Note : Before using this code please create a java project in eclipse and save the 
code inside NumberToString .java file inside src folder  


import java.util.Scanner;

public class NumberToString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

/*
                 Three arrays for corresponding String to the number
 one to print values of ones place 
 second to print values of tens place
 third is due to different naming of eleven series 
*/
                String[] ones = { "zero", "one", "two", "three", "four", "five", "six", 
                                         "seven", "eight", "nine" };
String[] tens = { "zero", "ten", "twenty", "thirty", "fourty", "fifety",
                                       "sixty", "seventy", "eighty","ninety" };
String[] teens = { "ten", "eleven", "twelve", "thirteen", "fourteen", 
                                         "fifteen", "sixteen", "seventeen","eighteen", "nineteen" };
                System.out.println("please enter any number below 1,00,00,000");
String num = sc.next();
                int a = num.length();
             
                // applying validation such that number above range will show error
if (a > 9) {
System.err.println("OOPS ! Out of range");
System.err.println("Try again by restarting program");
} else {
//converting String into array of bytes 
byte[] b = num.getBytes();
byte[] d = new byte[a];
/*
since in array b we get corresponding ascii value of String 
like for 0 it is 48 and for 1 it is 49
so to get integer back from ascii value array d is used and 
in below for loop we retrieve back the int value from ascii codes
and here array is reversed as we will be decreasing size of array
from end point we will read one value and print corresponding
                        string for it as we do in normal life we read a number from left 
                        to right
*/
for (int i = 0; i < a; i++) {
d[a - i - 1] = (byte) (b[i] - 48);
}
//performing operations regarding to size
while (a != 0) {
if (a == 1) {
System.out.print(ones[d[a - 1]] + " ");
a = a - 1;
} else if (a == 2) {
if (d[a - 1] == 1) {
System.out.print(teens[d[a - 2]] + " ");
a = a - 2;
//here size was decreased by 2 because in 
//case of teens two digit are read as single
} else if (d[a - 1] == 0) {
a = a - 1;
if (d[a - 1] == 0) {
a = a - 1;
}
} else {
System.out.print(tens[d[a - 1]] + " ");
if (d[a - 2] == 0) {
a = a - 2;
} else {
a = a - 1;
}
}
} else if (a == 3) {
if (d[a - 1] == 0) {
a = a - 1;
} else {
System.out.print(ones[d[a - 1]] 
 + " hundred ");
a = a - 1;
}
} else {
if (d[a - 1] == 0) {
a = a - 1;
// to discard 0 that comes on first place 
//as their is no significance of 0 at starting
} else if (a % 2 == 0) {
System.out.print(ones[d[a - 1]]);
if (a == 4) {
System.out.print(" thousand ");
} else if (a == 6) {
System.out.print(" lakh ");
} else if (a == 8) {
System.out.print(" crore ");
}
a = a - 1;
} else if (a % 2 == 1) {
if (d[a - 1] == 1) {
System.out.print(teens[d[a - 2]]);
} else {
System.out.print(tens[d[a - 1]] 
  + " " + ones[d[a - 2]]);
}
if (a == 5) {
System.out.print(" thousand ");
} else if (a == 7) {
System.out.print(" lakh ");
} else if (a == 9) {
System.out.print(" crore ");
}
a = a - 2;
}
}
}
}
}
}

No comments:

Post a Comment