Computer Science, asked by shrey8162, 1 year ago

C program to display the number in words using switch case where a sample input and output is given

Answers

Answered by akhilarul324
7

#include <stdio.h>

int main()

{

   int n, num = 0;

   printf("Enter any number to print in words: ");

   scanf("%d", &n);

   while(n != 0)

   {

       num = (num * 10) + (n % 10);

       n /= 10;

   }

   while(num != 0)

   {

       switch(num % 10)

       {

           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;

       }

       

       num = num / 10;

   }

   return 0;

}

Answered by vinod04jangid
0

Answer:

#include <stdio.h>

#include <math.h>

int main()

{

int n, num = 0, digits;

printf( "Enter any number to print in words: \n");

scanf("%d", &n);

digits = (int) log10(n);

while(n != 0)

{

num = (num * 10) + (n % 10);

n /= 10;

}

digits = digits - ((int) log10(num));

while(num != 0)

{

switch(num % 10)

{

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;

}

num /= 10;

}

//Print all trailing zeros

while( digits)

{

printf("Zero ");

digits--;

}

return 0;

}

Explanation:

step 1:write a main function and declare a varible inside it;

step 2:Take number from user

step 3:Find the number of digits.

step 4:Store reverse of digits n in num.

step 5:Extract last digit of number and print  in words till num becomes 0.

step 6:return

#SPJ2

Similar questions