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


Problem

Without using a switch case, how can you print a given number in words using the C programming language?

Solution

In this program, we are checking three conditions to print a two-digit number in words −

  • if(no<0 || no>99)

    entered number is not a two digit

  • else if(no==0)

    print first number as zero

  • else if(no>=10 && no<=19)

    Print single digit number in words

  • else if(no>=20 && no<=90)

         if(no%10 == 0)

    Print two-digit number in words

Program

 Live Demo

#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","ninty"};
   char *thirdno[]={"one","two","three","four","five","six","seven","eight","nine"};
   printf("enter a number:");
   scanf("%d",&no);
   if(no<0 || no>99)
      printf("enter number is not a two digit number
");    else if(no==0)       printf("the enter no is:%s
",firstno[no]);    else if(no>=10 && no<=19)       printf("the enter no is:%s
",firstno[no-10+1]);    else if(no>=20 && no<=90)       if(no%10 == 0)          printf("the enter no is:%s
",secondno[no/10 - 2]);    else       printf("the enter no is:%s %s
",secondno[no/10-2],thirdno[no%10-1]); return 0; }

Output

enter a number:79
the enter no is: seventy nine
enter a number:234
enter number is not a two digit number

Updated on: 05-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements