Converting digits to word format using switch case in C language

CServer Side ProgrammingProgramming

Problem

Is it possible to convert the given one or two-digit numbers into English words by using the C Programming language?

Solution

We can easily convert the given two-digit number into English word format with the help of a switch case. Not only two digits, but any number can also convert into English like a statement in C.

In this program, we will convert one or two-digit numbers into English word format.

Example

 Live Demo

#include<stdio.h>
int main(){
   int no;
   printf("enter any 1 or 2 digit number:");
   scanf("%d",&no);
   if(no<0 || no>=99) //finding out whether enter no is 2 digit or not
      printf("Invalid number");
   else{
      printf("entered number:");
      switch(no){
         case 0:printf("zero");
            break;
         case 10: printf("ten");
            break;
         case 11: printf("eleven");
            break;
         case 12: printf("twelve");
            break;
         case 13: printf("thirteen");
            break;
         case 14: printf("fourteen");
            break;
         case 15: printf("fifteen");
            break;
         case 16: printf("sixteen");
            break;
         case 17: printf("seventeen");
            break;
         case 18: printf("eighteen");
            break;
         case 19: printf("nineteen");
            break;
         default: switch(no/10) //first digit word{
            case 2: printf("twenty");
               break;
            case 3: printf("thirty");
               break;
            case 4: printf("forty");
               break;
            case 5: printf("fifty");
               break;
            case 6: printf("sixty");
               break;
            case 7: printf("seventy");
               break;
            case 8: printf("eighty");
               break;
            case 9: printf("ninty");
               break;
         }
         switch(no%10) //second digit word{
            case 1: printf("one");
               break;
            case 2: printf("tw0");
               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 any 1 or 2 digit number:83
entered number:eightythree

enter any 1 or 2 digit number:6
entered number:six

enter any 1 or 2 digit number:548
Invalid number
raja
Published on 05-Mar-2021 15:34:20
Advertisements