Print the given numeric value as words. It’s easy to do with switch using cases from 0-9 but challenge is without using them.
Input − N=900
Output − NINE ZERO ZERO
It is possible by creating array of pointers that contains 0-9 in words.
Algorithm
START Step 1 -> declare int variables num, i and array of pointer char *alpha with values {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"} Step 2 -> declare char array str[20] Step 3 -> call function itoa with parameters num,str,10 Step 4 -> Loop For i=0 and str[i]!=’\o’ and i++ Print alpha[str[i] - '0'] Step 5 -> End Loop STOP
Example
#include<stdio.h> #include<stdlib.h> int main() { int num, i; num=900; //lets take numeric value char *alpha[11] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}; char str[20]; itoa(num, str, 10); //this function will convert integer to alphabet for(i=0; str[i] != '\0'; i++) printf("%s ", alpha[str[i] - '0']); return 0; }
Output
If we run above program then it will generate following output
Enter an integer 900 NINE ZERO ZERO