C program to print number in words

Category: C Program

Learn how to write a C program to print a number in words. This article provides a detailed explanation and sample code for converting a number into words in C programming.

Printing a number in words is an interesting problem in programming that involves basic arithmetic operations and control structures. This article will guide you through writing a C program to convert a given number into words, providing a detailed explanation and sample code.

Steps to Print a Number in Words

To solve this problem, follow these steps:

  1. Input the Number: Read the number from the user.
  2. Reverse the Number: This helps in processing the number from the least significant digit to the most significant digit.
  3. Convert Each Digit to Words: Use arrays to map digits to their corresponding words.
  4. Print the Words: Output the words in the correct order.

Write a C program to print a number in words

Here's a C program to print a number in words:

#include <stdio.h>
#include <string.h>

void printDigitInWords(int digit) {
    switch(digit) {
        case 0: printf("Zero "); break;
        case 1: printf("One "); break;
        case 2: printf("Two "); break;
        case 3: printf("Three "); break;
        case 4: printf("Four "); break;
        case 5: printf("Five "); break;
        case 6: printf("Six "); break;
        case 7: printf("Seven "); break;
        case 8: printf("Eight "); break;
        case 9: printf("Nine "); break;
        default: printf("Invalid digit "); break;
    }
}

int main() {
    int number, reversedNumber = 0, digit;
    int originalNumber;

    // Input the number
    printf("Enter an integer: ");
    scanf("%d", &number);

    // Store the original number to handle the case of 0 separately
    originalNumber = number;

    // Reverse the number
    while (number != 0) {
        digit = number % 10;
        reversedNumber = reversedNumber * 10 + digit;
        number /= 10;
    }

    // Handle the case of 0
    if (originalNumber == 0) {
        printf("Zero");
    } else {
        // Print the digits in words
        while (reversedNumber != 0) {
            digit = reversedNumber % 10;
            printDigitInWords(digit);
            reversedNumber /= 10;
        }
    }

    return 0;
}

Output

Enter an integer: 4321
Four Three Two One

Explanation

  1. Input the Number: The program prompts the user to enter an integer. The scanf function reads the input number and stores it in the variable number.
  2. Store the Original Number: The original number is stored in originalNumber to handle the special case of 0.
  3. Reverse the Number:
    • The while loop continues as long as number is not equal to 0.
    • The last digit is extracted using the modulus operator (%). For example, if number is 123, digit = 123 % 10 will result in digit = 3.
    • The extracted digit is used to build the reversedNumber by multiplying reversedNumber by 10 and adding the digit.
    • The last digit is removed from number using integer division (/). For example, number = 123 / 10 results in number = 12.
  4. Handle the Case of 0: If the original number is 0, the program prints "Zero" and exits.
  5. Print the Digits in Words:
    • The while loop continues as long as reversedNumber is not equal to 0.
    • The last digit is extracted using the modulus operator (%), and the printDigitInWords function is called to print the digit in words.
    • The last digit is removed from reversedNumber using integer division (/).

Detailed Steps

  • Step 1: Input the Number
    • Use printf to prompt the user for input.
    • Use scanf to read the number from the user.
  • Step 2: Store the Original Number
    • Store the original number in a separate variable to handle the special case of 0.
  • Step 3: Reverse the Number
    • Use a while loop to continue processing as long as number is not zero.
    • Use the modulus operator (%) to get the last digit of the number.
    • Build the reversedNumber by multiplying the current reversedNumber by 10 and adding the extracted digit.
    • Remove the last digit from the number using integer division (/).
  • Step 4: Handle the Case of 0
    • If the original number is 0, print "Zero" and exit the program.
  • Step 5: Print the Digits in Words
    • Use a while loop to continue processing as long as reversedNumber is not zero.
    • Use the modulus operator (%) to get the last digit of the reversedNumber.
    • Call the printDigitInWords function to print the digit in words.
    • Remove the last digit from reversedNumber using integer division (/).

Example

For an input of 123, the program will:

  • Reverse the number to get 321.
  • Print "Three Two One".

For an input of 0, the program will:

  • Print "Zero".

Recommended Posts