A robust and lightweight C program that converts numerical inputs (integers) into their equivalent English word representation. It supports numbers up to 999,999,999,999 (Billions).
- Large Range Support: Converts numbers from units up to billions.
- Negative Number Handling: Detects and prefixes negative numbers with "Manfi" (Negative).
- Smart Logic for Teens: Correcty handles the "teen" numbers (11–19) which differ from standard decimal patterns.
- Modular Structure: Uses a matrix-based lookup table for efficient word retrieval.
- Group Formatting: Automatically appends scales like "Thousand", "Million", and "Billion".
The program processes the input number by breaking it down into groups of three digits (Triplets):
- Lookup Tables: A 2D character array stores words for units, tens, hundreds, and the unique 11-19 range.
- Matrix Mapping: It maps digits into a
4x4output matrix where each row represents a scale (Units, Thousands, Millions, Billions). - Digit Conversion: The
DigitConvertfunction handles specific English grammar rules, such as transforming "ten" + "one" into "eleven". - Output formatting: The matrix is traversed in reverse to print the words in the correct English reading order.
- A C compiler (e.g.,
GCC,Clang, orMSVC). - Standard C libraries:
<stdio.h>,<string.h>,<math.h>.
-
Clone the repository:
git clone https://github.com/amir-bigdeli-dev/covert-Number-to-Number-String-with-C.git cd covert-Number-to-Number-String-with-C -
Compile the code:
gcc project.c -o converter
-
Run the program:
./converter
Input:
enter a number that you want to convert: 1250450
For an input like 1,250,450, the output variable is populated as follows:
| Row | Scale (Index 0) | Units (Index 1) | Tens (Index 2) | Hundreds (Index 3) |
|---|---|---|---|---|
| [0] | NULL |
NULL |
"fifty" |
"four hundred" |
| [1] | "Thousand" |
NULL |
"fifty" |
"two hundred" |
| [2] | "Million" |
"one" |
NULL |
NULL |
| [3] | NULL |
NULL |
NULL |
NULL |
Code Representation:
// How the 'output' variable looks internally for 1,250,450
output[4][4] = {
{ NULL, "NULL", "fifty", "four hundred" }, // Base units
{ "Thousand", "NULL", "fifty", "two hundred" }, // Thousands group
{ "Million", "one", "NULL", "NULL" }, // Millions group
{ NULL, "NULL", "NULL", "NULL" } // Billions group
};And it reads from the end to the beginning.
Output:
one Million two hundred fifty Thousend four hundred fifty
- The current version supports numbers up to
$10^{12} - 1$ . - Ensure the input does not exceed the
long long intcapacity of your system.
Contributions are welcome! If you find a bug or have a suggestion for optimization (like adding "and" between hundreds and tens), feel free to open an issue or submit a pull request.
Developed with ❤️ by Amir Bigdeli