C program to print number in words
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:
- Input the Number: Read the number from the user.
- Reverse the Number: This helps in processing the number from the least significant digit to the most significant digit.
- Convert Each Digit to Words: Use arrays to map digits to their corresponding words.
- 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
- Input the Number: The program prompts the user to enter an integer. The
scanf
function reads the input number and stores it in the variablenumber
. - Store the Original Number: The original number is stored in
originalNumber
to handle the special case of 0. - Reverse the Number:
- The
while
loop continues as long asnumber
is not equal to0
. - The last digit is extracted using the modulus operator (
%
). For example, ifnumber
is123
,digit = 123 % 10
will result indigit = 3
. - The extracted digit is used to build the
reversedNumber
by multiplyingreversedNumber
by 10 and adding the digit. - The last digit is removed from
number
using integer division (/
). For example,number = 123 / 10
results innumber = 12
.
- The
- Handle the Case of 0: If the original number is 0, the program prints "Zero" and exits.
- Print the Digits in Words:
- The
while
loop continues as long asreversedNumber
is not equal to0
. - The last digit is extracted using the modulus operator (
%
), and theprintDigitInWords
function is called to print the digit in words. - The last digit is removed from
reversedNumber
using integer division (/
).
- The
Detailed Steps
- Step 1: Input the Number
- Use
printf
to prompt the user for input. - Use
scanf
to read the number from the user.
- Use
- 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 asnumber
is not zero. - Use the modulus operator (
%
) to get the last digit of the number. - Build the
reversedNumber
by multiplying the currentreversedNumber
by 10 and adding the extracted digit. - Remove the last digit from the number using integer division (
/
).
- Use a
- 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 asreversedNumber
is not zero. - Use the modulus operator (
%
) to get the last digit of thereversedNumber
. - Call the
printDigitInWords
function to print the digit in words. - Remove the last digit from
reversedNumber
using integer division (/
).
- Use a
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".