Introduction
In many applications, we need to represent numbers in the form of words. This is often used in scenarios such as writing cheques, displaying numbers in a human-readable form, or creating financial reports. In this guide, we will walk you through writing a simple C program that converts any number into its word representation.
Objective
The main objective of this C program is to take a numerical input from the user and convert that number into its corresponding word form. The program will be able to handle large numbers as well and provide accurate word representation for the user.
Code for Number to Words Converter
#include #include void convert_to_words(int n); void num_to_word(int num, char *result); char *ones[] = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; char *tens[] = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; char *thousands[] = { "", "Thousand", "Million", "Billion" }; void convert_to_words(int n) { if (n == 0) { printf("Zero\n"); return; } char result[1000] = ""; int i = 0; while (n > 0) { if (n % 1000 != 0) { char temp[1000]; num_to_word(n % 1000, temp); strcat(temp, " "); strcat(temp, thousands[i]); strcat(temp, " "); strcat(result, temp); } n /= 1000; i++; } printf("%s\n", result); } void num_to_word(int num, char *result) { if (num == 0) { return; } if (num > 99) { strcat(result, ones[num / 100]); strcat(result, " Hundred "); num = num % 100; } if (num > 19) { strcat(result, tens[num / 10]); strcat(result, " "); num = num % 10; } if (num > 0) { strcat(result, ones[num]); } } int main() { int number; printf("Enter a number: "); scanf("%d", &number); convert_to_words(number); return 0; }
Explanation of the Program
The program works by first checking if the number is zero and displaying “Zero” if true. Otherwise, the program divides the number into groups of thousands (e.g., thousands, millions) and converts each group into words. It uses two main arrays: ones[]
for numbers from 1 to 19, and tens[]
for multiples of ten like twenty, thirty, etc.
The convert_to_words()
function is responsible for handling the overall conversion, breaking the number down into groups and converting each part. The num_to_word()
function handles the conversion of each individual group of digits.
The program loops through the number, breaking it into chunks of three digits (e.g., hundreds, thousands, etc.) and appending the appropriate labels (e.g., “Thousand”, “Million”) to the result.
How to Run the Program
To run this program, follow these steps:
- Open a text editor and paste the code into a new file. Save it with a
.c
extension (e.g.,number_to_words.c
). - Compile the code using a C compiler like GCC. In the terminal, run:
gcc number_to_words.c -o number_to_words
- Run the compiled program using:
./number_to_words
. - Enter a number when prompted, and the program will output the number in words.