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
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 |
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 |
#define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); }
write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30
Print an integer using only putchar. Try doing it without using extra storage.
write a c-program to find gcd using recursive functions
How do you write a program which produces its own source code as its output?
void main() { static int i=i++, j=j++, k=k++; printf(“i = %d j = %d k = %d”, i, j, k); }
main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }
How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.
#define a 10 void foo() { #undef a #define a 50 } int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } explain the answer
to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD
main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }
main() { int i=400,j=300; printf("%d..%d"); }