Given an integer between 0-999 input by the user, print the English words corres
ID: 3532525 • Letter: G
Question
Given an integer between 0-999 input by the user, print the English words corresponding to that integer.
Similar to previous assignment, your program should print an ending when user choose to end this
program.
Example:
Enter the integer: 1
1 is one
Enter the integer: 11
11 is eleven
Enter the integer: 111
111 is one hundred eleven
Enter the integer: 1111
out of range
Enter the integer: ^D
Thanks for using. This ends the program!
There's two issues with the program below that I got from here (I made some small changes), it doesn't print out the exact values, for example if I try to input 9999 it printouts nine thousand, or if I try to input anything less than 1000 it doesn't print out anything at all. And the second this is I don't know how enter the ^D to end the program.
#include<stdio.h>
#include<math.h>
void print(int n)
{
int num=n;
//Declare static strings
char* lessThan20[] = {
"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen"
};
char* hundred = " hundred ";
char* thousand = " thousand";
char* tens[] = { "zero","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
if(num < 0)
printf(" Its a negative number");
num = abs(num);
n = num/ 1000;
if(n > 0)
printf(" %s thousand ",lessThan20[n]);
num=00;
n = num / 100;
if(n > 0)
printf(" %s hundred ",lessThan20[n]);
num%=100;
if(num >=20)
{
n=num/10;
if(n>0)
printf(" %s ",tens[n]);
}
else if(num>=10)
{
printf(" %s ",lessThan20[num]);
return;
}
num%=10;
if(num > 0)
printf(" %s ",lessThan20[num]);
}
int main()
{
int n;
printf("Enter a number [Range from 0 - 9999 OR 0 to Exit]: ");
scanf("%d",&n);
while(n!= 0)
{
printf(" %d is",n);
//declare member function
print(n);
printf(" ");
//cout <<"Enter a number [ Range 0 - 9999 (or) 0 to exit]: ";
scanf("%d",&n);
}
//pause system
return 0;
}
Explanation / Answer
Program to Convert Numbers into Words
#include<stdio.h>
void pw(long,char[]);
char *one[]={" "," one"," two"," three"," four"," five"," six"," seven","
eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","
fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","
seventy"," eighty"," ninety"};
void main()
{
long n;
clrscr();
printf("
Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than 0");
else
{
pw((n/10000000),"crore");
pw(((n/100000)0),"lakh");
pw(((n/1000)0),"thousand");
pw(((n/100)),"hundred");
pw((n0)," ");
}
getch();
}
void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],one[n]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.