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 |
main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above
main() { char a[4]="HELL"; printf("%s",a); }
how can i cast a char type array to an int type array
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines
struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }
29 Answers IBM, TCS, UGC NET, Wipro,
how to delete an element in an array
# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }
why java is platform independent?
main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }