In this blog post, we learn how to write a C program to convert a given number to words?. We will write the C program to convert a given number into words. How to display input number in words using loop in C programming. Write a C program to input a number from the user and convert it into words using a loop. Logic to convert a given number to words in C programming.
Example,
Input: 575655 Output: five lakh seventy five thousand six hundred and fifty five Input: 27 Output: twenty seven Input: 234677 Output: two lakh thirty four thousand six hundred and seventy seven
C program to convert a given number to words:
Before seeing the program, let see a representation that shows the place value chart for any 9 digits positive integer number.
9 8 7 6 5 4 3 2 1 | | | | | | | | |__ ones' place | | | | | | | |__ __ tens' place | | | | | | |__ __ __ hundreds' place | | | | | |__ __ __ __ thousands' place | | | | |__ __ __ __ __ tens thousands' place | | | |__ __ __ __ __ __ hundred thousands' place | | |__ __ __ __ __ __ __ one millions' place | |__ __ __ __ __ __ __ __ ten millions' place |__ __ __ __ __ __ __ __ __ hundred millions' place
The idea is to divide the number into individual digits based on the above place value chart and handle them starting from the most significant digit. The below program will work for any 9 digit number. You can increase the number of digit by changing data types long to unsigned long long int.
#include<string.h> #include<stdio.h> #define BUF_SIZE 20 // strings at index 0 is not used, it is to make array // indexing simple char* one[] = { "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " }; // strings at index 0 and 1 are not used, they is to // make array indexing simple char* ten[] = { "", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " }; char * numToWords(int n, char* s, char *str, int len) { memset(str,0,len); // if n is more than 19, divide it if (n > 19) { strcat(str,ten[n / 10]); strcat(str,one[n % 10]); } else { strcat(str,one[n]); } // if n is non-zero if (n) { strcat(str,s); } return str; } // Function to print a given number in words char* convertToWords(long n, char *out) { char str[BUF_SIZE] = {0}; // handles digits at ten millions and hundred // millions places (if any) strcat(out, numToWords((n / 10000000), "crore ",str,BUF_SIZE)); // handles digits at hundred thousands and one // millions places (if any) strcat(out, numToWords(((n / 100000) % 100), "lakh ",str,BUF_SIZE)); // handles digits at thousands and tens thousands // places (if any) strcat(out, numToWords(((n / 1000) % 100), "thousand ",str,BUF_SIZE)); // handles digit at hundreds places (if any) strcat(out, numToWords(((n / 100) % 10), "hundred ",str,BUF_SIZE)); //Increase code readability if (n > 100 && n % 100) { strcat(out, "and "); } // handles digits at ones and tens places (if any) strcat(out, numToWords((n % 100), "",str,BUF_SIZE)); return out; } int main() { //Get input number from user long num; char str[60] = {0}; printf("Enter any number: "); scanf("%ld", &num); // convert given number in words printf( "%s",convertToWords(num,str)); return 0; }
Output:
Enter any number: 4567890
forty five lakh sixty seven thousand eight hundred and ninety
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Use of strcat in C.
- Use of strncat in C.
- Stringizing operator (#) in C.
- C String Literals with Its Types.
- How to use atoi() and how to make your own atoi()?
- Token Pasting Operator in C/C++ programming.
- C program to trim trailing white spaces from a string.
- C Program to convert uppercase string to lowercase string
- String Interview Questions in C.