Verbalize a Number The goal of this programming project is spell out large numbe
ID: 3919017 • Letter: V
Question
Verbalize a Number
The goal of this programming project is spell out large numbers in English. Here, the number will be between 0 and 999,999,999 (both inclusive). You should break this problem down in multiple simple functions that work together.
Step 1: Spelling a single digit
The first step involves writing a function called sayDigit that takes a number from 0 to 9 and returns a text string for that number in English. For example, the function should return the string "five" for the input number 5. There are different ways of solving it such as a chained conditional or a list of strings.
Before moving to step 2, you should test your function on different numbers by adding the following two lines to the end of your program (these lines should be changed later to support larger numbers):
Step 2: Spelling small numbers
Before moving on the hundreds, thousands and millions, you should write a second function (such as sayTeen) that can handle numbers between 0 and 19. Here, you can re-use your previously defined function sayDigit to handle the case that the number is less than 10 and include additional Python code for numbers between 11 and 19.
Again, you should test your function to see that how it works before continuing to step 3.
Step 3: Spelling large numbers
In order to solve the problem for large numbers, you should continue the same process of defining more and more functions to break down the problem into smaller parts that can be handled by other functions. This way, you can handle two-digit numbers up to 99, then three digit numbers, thousands and finally millions. Also, you should print an error message and quit the program if the number is larger than 999,999,999.
Hint: You might want to use the integer division (//) and remainder (%) operators for this problem.
Example Interactions (each interaction is running the program once):
(Here, lines starting with > indicate output from the program and lines starting with < indicate input.)
Explanation / Answer
import java.util.*;
import java.io.*;
import java.lang.*;
public class NumberToString
{
public static void main(String ar[])
{ int n,t;
String str;
Scanner s=new Scanner(System.in);
str=s.nextLine();
n=(int )Integer.parseInt(str);
int len=str.length();
int[] a=new int[len];
int d=n;
while (d!=0)
{
len--;
a[len]=d%10;
d=d/10;
}
int j=len;
while (j!=str.length())
{
j++;
}
len=str.length();
int k=0,l=0;
while (k!=str.length())
{
if (len==6)
{
switch(a[k])
{
case 1:
System.out.print("One Lakh");
break;
case 2:
System.out.print("Two Lakh");
break;
case 3:
System.out.print("Three Lakh");
break;
case 4:
System.out.print("Four Lakh");
break;
case 5:
System.out.print("Five Lakh");
break;
case 6:
System.out.print("Six Lakh");
break;
case 7:
System.out.print("Seven Lakh");
break;
case 8:
System.out.print("Eight Lakh");
break;
case 9:
System.out.print("Nine Lakh");
break;
}
k++;
}
if(len==5)
{
switch(a[k])
{
case 1:
switch (a[k+1])
{
case 0:
System.out.print(" Ten Thousand");
break;
case 1:
System.out.print(" Eleven Thousand");
break;
case 2:
System.out.print(" Twelve Thousand");
break;
case 3:
System.out.print(" Thirteen Thousand");
break;
case 4:
System.out.print(" Fourteen Thousand");
break;
case 5:
System.out.print(" Fifteen Thousand");
break;
case 6:
System.out.print(" Sixteen Thousand");
break;
case 7:
System.out.print(" Seventeen Thousand");
break;
case 8:
System.out.print(" Eighteen Thousand");
break;
case 9:
System.out.print(" Nintheen Thousand");
break;
}
break;
case 2:
if (a[k+1]==0)
System.out.print(" Twenty Thousand");
else
System.out.print("Twenty");
break;
case 3:if (a[k+1]==0)
System.out.print(" Thirty Thousand");
else
System.out.print("Thirty");
break;
case 4:
if (a[k+1]==0)
System.out.print(" Fourty Thousand");
else
System.out.print(" Fourty");
break;
case 5:
if (a[k+1]==0)
System.out.print(" Fifty Thousand");
else
System.out.print(" Fifty");
break;
case 6:if (a[k+1]==0)
System.out.print(" Sixthy Thousand");
else
System.out.print(" Sixty");
break;
case 7:
if (a[k+1]==0)
System.out.print(" Seventhy Thousand");
else
System.out.print(" Seventhy");
break;
case 8:
if (a[k+1]==0)
System.out.print(" Eighty Thousand");
else
System.out.print(" Eighty");
break;
case 9:
if (a[k+1]==0)
System.out.print(" Ninty Thousand");
else
System.out.print(" Ninty");
break;
}
if (a[k]!=1)
{
if (a[k+1]!=0)
{
k=k+1;
}
}
else
{
k=k+2;
}
}
if(len==4)
{
switch(a[k])
{
case 1:
System.out.print(" One Thousand");
break;
case 2:
System.out.print(" Two Thousand");
break;
case 3:
System.out.print(" Three Thousand");
break;
case 4:
System.out.print(" Four Thousand");
break;
case 5:
System.out.print(" Five Thousand");
break;
case 6:
System.out.print(" Six Thousand");
break;
case 7:
System.out.print(" Seven Thousand");
break;
case 8:
System.out.print(" Eight Thousand");
break;
case 9:
System.out.print(" Nine Thousand");
break;
}
k++;
}
else if(len==3)
{
switch(a[k])
{
case 1:
System.out.print(" One Hundered");
break;
case 2:
System.out.print(" Two Hundered");
break;
case 3:
System.out.print(" Three Hundered");
break;
case 4:
System.out.print(" Four Hundered");
break;
case 5:
System.out.print(" Five Hundered");
break;
case 6:
System.out.print(" Six Hundered");
break;
case 7:
System.out.print(" Seven Hundered");
break;
case 8:
System.out.print(" Eight Hundered");
break;
case 9:
System.out.print(" Nine Hundered");
break;
}
//if (a[k]!=0 && a[k+1]==0 && a[k+2]==0)
//{
// System.out.print(" Zero Zero");
//}
k++;
}
else if(len==2)
{
switch(a[k])
{
case 1:
switch(a[k+1])
{
case 0:
System.out.print(" Ten");
break;
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(" Nintheen");
break;
}
k++;
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(" Ninthy");
break;
default :break;
}
k++;
}
else if (len==1)
{
switch(a[k])
{
case 0:
System.out.print("Zero");
break;
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;
default:break;
}
k++;
}
if (len==5 && a[k]==1 && a[k+1]!=0)
{
len=len-2;
}
else
{
len--;
}
//System.out.print("length="+len);
l=k;
t=0;
while (l!=str.length())
{
if (a[l]!=0)
{t=1;
}
l++;
}
if (k==2&&t==0)
{ //System.out.println("Hundered");
break;
}
else if (k==1&&t==0)
{
break;
}
}System.out.println(" ");}
}
This Code Gives The Correct Output below 1000000 .Hope i will get some points.Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.