pls anyone can help me to write a code to print the values
in words for any value.Example:1034 to print as "one
thousand and thirty four only"

Answers were Sorted based on User's Feedback



pls anyone can help me to write a code to print the values in words for any value.Example:1034 to ..

Answer / pavan_mustyala

Hi, This code works for 4 digit numbers(may be with some
minor exceptions). But i am trying a generic approach and
shall update very soon with more nicer solution.
/*************/
#include
char *arr1[10] =
{"One","Two","Three","Four","Five","Six","Seven","Eight","Ni
ne", "Ten"};
char *arr2[10] =
{"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen"
,"Seventeen","Eighteen","Nineteen"};
char *arr3[10] =
{"Ten","Twenty","Thirty","Fourty","Fifty","sixty","Seventy",
"Eighty","Ninety","Hundred"};
char *arr4[10] = {"Hundred","Thousand"};

int CountGlobal;

int func(int);
void printWord(int, int);

int main(int argc, char* argv[])
{
int num = 2022;
int temp = num;
int count = 0;

// First count the number of digits in the given
number
while(temp)
{
temp /= 10;
count++;
}

CountGlobal = count;

while(count && num)
{
num = func(num);
count--;
}

return 0;
}

// Functions to print digits in words
int func(int num)
{
int temp = num;
int count = 0;


while(temp > 9)
{
temp /= 10;
count++;
}

printWord(temp,count+1);

while(count)
{
temp *= 10;
count--;
}

return(num - temp);
}

void printWord(int num, int count)
{

switch(count)
{
case 0:
//printf("%s", arr[num-1]);
break;
case 1:
printf("%s", arr1[num-1]);
break;
case 2:
printf("%s ", arr3[num-1]);
//printf("%s ", arr3[1]);
break;
case 3:
printf("%s ", arr1[num-1]);
printf("%s ", arr4[0]);
break;
case 4:
printf("%s ", arr1[num-1]);
printf("%s ", arr4[1]);
break;
case 5:
//printf("%s", arr[num-1]);
break;
default:
break;
}
}
/**********/

Is This Answer Correct ?    4 Yes 1 No

pls anyone can help me to write a code to print the values in words for any value.Example:1034 to ..

Answer / vin

not sure wether this is the best in performance.
need to use switch case.

string = "";
for lengh of the number
{
switch(char)
{
case 1:
string = string + one;
case 2:
string = string + two;
case 3:
string = string + three;
}

{

but that gives only letters into numbers.
for ex 1043 will be onezerothreefour

this does not end here, please modify or add.

as this has to be transformed into numebrsystem

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More C Code Interview Questions

write a program for area of circumference of shapes

0 Answers  


how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!

8 Answers   GATE,


main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }

2 Answers   Wipro,


#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }

1 Answers  


#define SQR(x) x * x main() { printf("%d", 225/SQR(15)); } a. 1 b. 225 c. 15 d. none of the above

3 Answers   HCL,






main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

5 Answers   HCL,


char inputString[100] = {0}; To get string input from the keyboard which one of the following is better? 1) gets(inputString) 2) fgets(inputString, sizeof(inputString), fp)

1 Answers  


Write a program to receive an integer and find its octal equivalent?

7 Answers  


int a = 10 + 10 .... ,... A = A * A What would be the value of A? The answer is 120!! Could anyone explain this to me.

2 Answers   Bosch, eInfochips, HCL, IHCL,


How can you relate the function with the structure? Explain with an appropriate example.

0 Answers  


‎#define good bad main() { int good=1; int bad=0; printf ("good is:%d",good); }

2 Answers  


Sir... please give some important coding questions asked by product companies..

0 Answers  


Categories