Sure, I can help you create a program that converts an integer into its corresponding English words without using any external libraries. Below is a Python implementation that handles numbers up to the billions. You can extend it further if needed.
Python Implementation
def number_to_words(n):
"""
Convert a number into words.
"""
if n == 0:
return "Zero"
# Define words for numbers
ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninety"]
thousands = ["", "Thousand", "Million", "Billion"] # Extend as needed
def three_digit_word(num):
"""
Convert a number less than 1000 to words.
"""
word = ""
hundred = num // 100
remainder = num % 100
if hundred != 0:
word += ones[hundred] + " Hundred "
if remainder >= 10 and remainder < 20:
word += teens[remainder - 10] + " "
else:
ten = remainder // 10
one = remainder % 10
if ten != 0:
word += tens[ten] + " "
if one != 0:
word += ones[one] + " "
return word
word = ""
i = 0 # Index for thousands
while n > 0:
current_chunk = n % 1000
if current_chunk != 0:
chunk_word = three_digit_word(current_chunk)
if thousands[i] != "":
chunk_word += thousands[i] + " "
word = chunk_word + word
n = n // 1000
i += 1
return word.strip()
# Example Usage
if __name__ == "__main__":
try:
number = int(input("Enter an integer: "))
if number < 0:
print("Negative numbers are not supported.")
else:
print(number_to_words(number))
except ValueError:
print("Invalid input. Please enter a valid integer.")
How It Works
-
Basic Definitions:
ones: Words for single-digit numbers.
teens: Words for numbers between 10 and 19.
tens: Words for multiples of ten.
thousands: Suffixes for thousands, millions, billions, etc.
-
Function three_digit_word(num):
- Converts a number less than 1000 into words.
- Handles hundreds, tens, and ones places.
- Accounts for special cases like teens.
-
Main Logic:
- The number is processed in chunks of three digits (e.g., thousands, millions).
- For each chunk,
three_digit_word is called to get the word representation.
- The appropriate suffix from
thousands is appended.
- The resulting words are concatenated to form the final output.
-
Input Handling:
- The program prompts the user to enter an integer.
- It checks for negative numbers and invalid inputs.
Examples
Handling Larger Numbers
The current implementation handles numbers up to the billions. To support larger numbers, you can extend the thousands list with more suffixes like "Trillion", "Quadrillion", etc.
thousands = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion"]
Notes
- The program does not support negative numbers. You can modify it to handle negatives if needed.
- The program assumes the user inputs a valid integer. It includes basic error handling for non-integer inputs.
- All words are capitalized as per the examples. You can modify the casing based on your requirements.
Conclusion
This Python program efficiently converts integers into their English word representations without relying on any external libraries. It's highly customizable and can be extended to handle even larger numbers by adding more suffixes to the thousands list.