Can you convert this c++ program into c: //program to convert integers into engl
ID: 662698 • Letter: C
Question
Can you convert this c++ program into c:
//program to convert integers into english texts. Up to 999999
#include
using namespace std;
void expand(int);
int main(){
int num;
while(true){
cout << "Enter number in figures; use a negative value to stop. Number: ";
cin >> num;
if(num < 0) break;
expand(num);
cout << " ";
}
}
void expand(int value){
const char * const ones[20] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
if(value >= 100000){
expand(value / 100000);
cout << " lakh ";
if(value % 100000){
expand(value % 100000);
}
}
if(value >= 1000){
expand(value / 1000);
cout << " thousand ";
if(value % 1000){
expand(value % 1000);
}
}
else if(value >= 100){
expand(value / 100);
cout << " hundred ";
if(value % 100){
expand (value % 100);
}
}
else if(value >= 20){
cout << tens[value / 10];
if(value % 10){
cout << "-";
expand(value % 10);
}
}
else{
cout << ones[value];
}
}
Explanation / Answer
#include<stdio.h>
#include < conio.h >
void main()
{
long num,div,n1;
int flag,digit,pos,tot_dig;
clrscr();
printf(" Enter number: ");
scanf("%ld",& num);
if(num==0)
{
printf("Zero ");
exit(0);
}
if(num>99999)
{
printf("please enter number between 0 and 100000 ");
exit(0);
}
tot_dig = 0;
div = 1;
n1 = num;
while ( n1 > 9 )
{
n1 = n1 / 10;
div = div * 10;
tot_dig++;
}
tot_dig++;
pos = tot_dig;
while ( num != 0 )
{
digit= num / div;
num = num % div;
div = div / 10;
switch(pos)
{
case 2:
case 5: if ( digit == 1 )
flag = 1;
else
{
flag = 0;
switch(digit)
{
case 2: printf("twenty ");break;
case 3: printf("thirty ");break;
case 4: printf("forty ");break;
case 5: printf("fifty ");break;
case 6: printf("sixty ");break;
case 7: printf("seventy ");break;
case 8: printf("eighty ");break;
case 9: printf("ninty ");
}
}
break;
case 1:
case 4: if ( flag == 1 )
{
flag = 0;
switch(digit)
{
case 0 : printf("ten ");break;
case 1 : printf("eleven ");break;
case 2 : printf("twelve ");break;
case 3 : printf("thirteen ");break;
case 4 : printf("fourteen ");break;
case 5 : printf("fifteen ");break;
case 6 : printf("sixteen ");break;
case 7 : printf("seventeen ");break;
case 8 : printf("eighteen ");break;
case 9 : printf("ninteen ");
}
}
else
{
switch(digit)
{
case 1 : printf("one ");break;
case 2 : printf("two ");break;
case 3 : printf("three ");break;
case 4 : printf("four ");break;
case 5 : printf("five ");break;
case 6 : printf("six ");break;
case 7 : printf("seven ");break;
case 8 : printf("eight ");break;
case 9 : printf("nine ");
}
}
if ( pos == 4 )
printf("thousand ");
break;
case 3:
if ( digit> 0 )
{
switch(digit)
{
case 1 : printf("one ");break;
case 2 : printf("two ");break;
case 3 : printf("three ");break;
case 4 : printf("four ");break;
case 5 : printf("five ");break;
case 6 : printf("six ");break;
case 7 : printf("seven ");break;
case 8 : printf("eight ");break;
case 9 : printf("nine ");
}
printf("hundred ");
}
break;
}
pos--;
}
if ( pos == 4 && flag == 0)
printf("thousand");
else
if ( pos == 4 && flag == 1)
printf("ten thousand");
if ( pos == 1 && flag == 1 )
printf("ten ");
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.