Write a C program to print numbers in words using elseif statements

In C programming, we can convert numbers into their word equivalents using else-if statements instead of switch cases. This approach is particularly useful for handling two-digit numbers by breaking them into tens and units places.

Syntax

if (condition1) {
    // statements
} else if (condition2) {
    // statements
} else if (condition3) {
    // statements
} else {
    // default statements
}

Algorithm

The program uses multiple else-if conditions to handle different number ranges −

  • Range validation: Check if number is between 0-99
  • Special case: Handle zero separately
  • Teens handling: Numbers 10-19 have unique word forms
  • Tens handling: Numbers 20-90 divisible by 10
  • Compound numbers: Combine tens and units for other numbers

Example

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

int main() {
    int no;
    char *firstno[] = {"zero", "ten", "eleven", "twelve", "thirteen", 
                       "fourteen", "fifteen", "sixteen", "seventeen", 
                       "eighteen", "nineteen"};
    char *secondno[] = {"twenty", "thirty", "forty", "fifty", "sixty", 
                        "seventy", "eighty", "ninety"};
    char *thirdno[] = {"one", "two", "three", "four", "five", "six", 
                       "seven", "eight", "nine"};
    
    printf("Enter a number: ");
    scanf("%d", &no);
    
    if (no < 0 || no > 99) {
        printf("Entered number is not a two digit number
"); } else if (no == 0) { printf("The entered number is: %s
", firstno[no]); } else if (no >= 10 && no <= 19) { printf("The entered number is: %s
", firstno[no - 10 + 1]); } else if (no >= 20 && no <= 99) { if (no % 10 == 0) { printf("The entered number is: %s
", secondno[no/10 - 2]); } else { printf("The entered number is: %s %s
", secondno[no/10 - 2], thirdno[no%10 - 1]); } } else { printf("The entered number is: %s
", thirdno[no - 1]); } return 0; }

Output

Enter a number: 79
The entered number is: seventy nine

How It Works

  1. Array initialization: Three arrays store word representations for different number ranges
  2. Range validation: First condition checks if number is within 0-99 range
  3. Special cases: Zero is handled separately using firstno[0]
  4. Teens (10-19): Use firstno array with offset calculation (no-10+1)
  5. Multiples of ten: Use secondno array with index (no/10-2)
  6. Other numbers: Combine tens and units using both secondno and thirdno arrays

Key Points

  • The program handles two-digit numbers (0-99) effectively using else-if chains
  • Array indexing is crucial − careful offset calculations prevent array bounds errors
  • The modulo operator (%) extracts the units digit for compound numbers
  • Integer division (/) extracts the tens digit for array indexing

Conclusion

Using else-if statements provides a structured approach to convert numbers into words. This method offers better readability than nested switches and efficiently handles different number ranges through conditional branching.

Updated on: 2026-03-15T13:45:23+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements