program in c++ to input digits and print in words



program in c++ to input digits and print in words..

Answer / a_l_soni

#include "stdafx.h"

void PrintDigit (int a);
void PrintTens (int a);
int g_set = 0;

int _tmain(int argc, _TCHAR* argv[])
{
int a = 1216;
int b = a;
int iValue = 1000;
for (int x = iValue; x >= 1 ; x=x/10)
{
int t = b/x;
switch (x)
{
case 1000:
PrintDigit (t);
printf (" Thousand ");
break;
case 100:
PrintDigit (t);
printf (" Hundred ");
break;
case 10:
PrintTens (b);
break;
case 1:
if (g_set == 0)
{
PrintDigit (t);
}
}
b = b%x;
}

getchar ();
return 0;
}

void PrintDigit (int a)
{
switch (a)
{
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");
}
}

void PrintTens (int a)
{
int x = a / 10;
if (x > 1)
{
int b = a % 10;
switch(x)
{
case 2:
printf ("Twenty ");
break;
case 3:
printf ("Thirty ");
break;
case 4:
printf ("Fourty ");
break;
case 5:
printf ("Fifty ");
break;
case 8:
printf ("Eighty ");
break;
case 6:
case 7:
case 9:
PrintDigit (x);
printf ("ty ");
break;
}
}
else
{
printf ("and ");
g_set = 1;
int b = a % 10;
switch(b)
{
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 8:
printf ("Eighteen");
break;
case 6:
case 7:
case 9:
PrintDigit (b);
printf ("teen ");
break;
}
}
}


To increase the number of digits, application can handle, increase the value of 'iValue' in main () and add an additional case for the value in the main, above 'case 1000:'.

Please vote and let me know....

Is This Answer Correct ?    14 Yes 11 No

Post New Answer

More C++ General Interview Questions

how to explain our contribution in the project?

0 Answers   Wipro, Yahoo,


How do you establish a has-a relationship?

0 Answers  


Why pointer is used in c++?

0 Answers  


What is an incomplete type in c++?

0 Answers  


How can you quickly find the number of elements stored in a a) static array b) dynamic array ?

5 Answers   Lucent,






founder of c++

7 Answers   Microtek, TCS, TeleCommand,


when can we use virtual destructor?

7 Answers   HCL, HP, Virage Logic,


When we use Abstract Class and when we use Interface?where we will implement in real time?

0 Answers  


What is the best c c++ compiler for windows?

0 Answers  


Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].

0 Answers  


What is an adaptor class in c++?

0 Answers  


What are the advantages of using a pointer?

0 Answers  


Categories