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

CServer Side ProgrammingProgramming

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\n");
   else if(no==0)
      printf("the enter no is:%s\n",firstno[no]);
   else if(no>=10 && no<=19)
      printf("the enter no is:%s\n",firstno[no-10+1]);
   else if(no>=20 && no<=90)
      if(no%10 == 0)
         printf("the enter no is:%s\n",secondno[no/10 - 2]);
   else
      printf("the enter no is:%s %s\n",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
raja
Published on 05-Mar-2021 16:37:06
Advertisements