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:

  1. Open a text editor and paste the code into a new file. Save it with a .c extension (e.g., number_to_words.c).
  2. Compile the code using a C compiler like GCC. In the terminal, run: gcc number_to_words.c -o number_to_words
  3. Run the compiled program using: ./number_to_words.
  4. Enter a number when prompted, and the program will output the number in words.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)