Skip to content

amir-bigdeli-dev/covert-Number-to-Number-String-with-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Number to Words Converter (C Implementation)

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).

🚀 Features

  • 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".

🛠 How it Works

The program processes the input number by breaking it down into groups of three digits (Triplets):

  1. Lookup Tables: A 2D character array stores words for units, tens, hundreds, and the unique 11-19 range.
  2. Matrix Mapping: It maps digits into a 4x4 output matrix where each row represents a scale (Units, Thousands, Millions, Billions).
  3. Digit Conversion: The DigitConvert function handles specific English grammar rules, such as transforming "ten" + "one" into "eleven".
  4. Output formatting: The matrix is traversed in reverse to print the words in the correct English reading order.

📋 Prerequisites

  • A C compiler (e.g., GCC, Clang, or MSVC).
  • Standard C libraries: <stdio.h>, <string.h>, <math.h>.

⚙️ Installation & Usage

  1. 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
  2. Compile the code:

    gcc project.c -o converter
  3. Run the program:

    ./converter

📸 Example

Input:

enter a number that you want to convert: 1250450

Memory Mapping Example

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

⚠️ Limitations

  • The current version supports numbers up to $10^{12} - 1$.
  • Ensure the input does not exceed the long long int capacity of your system.

🤝 Contributing

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

About

My project in ZNU University

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages