C Program to Print Digits of Number in Words

The C Program to Print Digits of a Number in Words is a valuable exercise that introduces beginners to string manipulation, conditional statements, and basic arithmetic operations in C programming.

Given a positive number N, we have to print the digits of N in words in Most significant digit(MSD) to least significant digit(LSD) sequence.

For Example

Input Number : 2345
Output : Two Three Four Five


Importance of Practicing this Program for Beginners

  • Conditional Statements : Implementing this program helps beginners practice using conditional statements (if-else) to control program flow based on certain conditions

  • Basic Arithmetic Operations : The program involves basic arithmetic operations, such as division and modulus, which are fundamental to many programming tasks.

  • Enhanced Logic Skills: : Implementing this program enhances logical thinking skills by challenging beginners to convert numerical values to their word representations

  • Preparation for Real-world Applications : The skills and concepts learned from this program prepare beginners for real-world applications, such as writing programs that convert numerical data to text for display purposes.

C program to print digits of a number in words

In this program, we first take a positive number as input from user using scanf function. Then we reverse the number as we want to print the most significant digits first(from leftmost digit to rightmost). After reversing, we remove rightmost digit one by one and pass it to "printDigit" function. Function "printDigit" that takes a positive number less than 10 as input and print it words using a switch statement.

#include<stdio.h>  

void printDigit(int digit);
int main() {
    int reverse = 0, digit, num, mod;
    printf("Enter a positive Integer\n");
    scanf("%d", &num);

    /* reverse the input number */
    while (num > 0) {
        reverse = (reverse * 10) + num%10;
        num /= 10;
    }
    num = reverse;

    while (num > 0) {
        digit = num % 10;
        printDigit(digit);
        num = num / 10;
    }
 ]
    return 0;
}

void printDigit(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;
    }
}
Output
Enter a positive Integer
2401
Two Four Zero One

Tips for Writing this Program in C

  • Boundary Conditions : Handle boundary conditions, such as zero and negative numbers, gracefully.

  • Error Handling : Implement error handling for invalid input cases, such as non-numeric characters.

  • Efficient Memory Usage : Use memory efficiently when storing and manipulating strings.

  • Looping Constructs : Use looping constructs (such as for loops) to iterate through the digits of the input number.

Conclusion

In conclusion, the C Program to Print Digits of a Number in Words is a valuable exercise that introduces beginners to string manipulation, conditional statements, and basic arithmetic operations in C programming. Practicing this program is important for beginners to develop a solid understanding of these concepts, which are essential for more advanced programming tasks.

Related Topics
C program to find product of digits of a number using while loop
C program to find sum of least and most significant digits of a number
C program to add digits of a number
C program to count number of digits in an integer
C program to find sum of digits of a number using recursion
C program to reverse a number using recursion
C program to find hcf and lcm of two numbers
C program to check armstrong number
C program to convert string to integer
List of all C programs