Write a C program to print numbers in word. In this article, we will learn how to print a number in a word using a switch case and a while loop.
Sample Input
Enter Number: 7657
Sample Output
Number in words: seven six five seven
Algorithm to print numbers in words
- Declare one variable and initialize the second variable.
- Then take an input.
- Take two while loops and in one loop give the condition of reverse number in another loop create a switch case in it.
- Give conditions and cases in the switch case.
C program to print numbers in words
#include<stdio.h> void main (){ int n; int num=0; printf("entre any number to print in word"); 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; } }
Explanation of the source code
Step 1: start.
Step 2: Create a header file and include the library (studio. h).
Step 3: Create the main function.
Step 4: Then declare a variable ( n ) and initialize the second variable ( num=0 ).
Step 5: Then take input with the help of scanf for the number taken from the user to print into word.
Step 6: Create two while loop one for the condition of reverse number and another for switch case
- In first loop give condition of reverse number ( (num*10)+(n%10)and num/=10 ) .
- In the second loop take the switch case: In a while loop give num!=0 give this condition, Switch case give num!=0 this condition.
- Then create the ten cases in the switch case as shown in the code from line number 15 to 43.
Step 7: Then give ( num=num/10 ) this condition in the last after close switch case.
Step 8: End.
Output
In this way, we learned how to write a C program to print numbers in words.