Here's a C program to change an integer to words with output and proper explanation. This program makes use of C concepts like For loop, While loop, Arrays, Switch Case, Break and Modulus.
# include <stdio.h> # include <conio.h> void main() { long n, a[10], i, c = 0 ; clrscr() ; printf("Enter a number : ") ; scanf("%ld", &n) ; while(n > 0) { a[c] = n % 10 ; n = n / 10 ; c++ ; } printf("\n") ; for(i = c - 1 ; i >= 0 ; i--) { switch(a[i]) { 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 ; } } getch() ; }
Output of above program is
Enter a number : 123
ONE TWO THREE
Explanation of above program
First take a look at all the variables. All variables are of Long Data Type-
- n - is the number that user enters.
- a[10] - is an array of size 10 and is used to store each digit of the above number n at separate indexes.
- i - is the loop variable.
- c - is a counter variable which is initialized to zero at the start of the program.
First we ask the user to enter a number (n can contain up to 10 digits for now) and stores it in variable n. Then using while loop, we're storing each digit of number n inside the array a[10]. Remember each digit is stored at a separate index. After the while loop the array a[10] will contain every digit of n but in reverse order e.g. if n = 123 then a[0] = 3, a[1] = 2 and a[2] = 1. (How to Reverse an Integer. Read it here.)
After this we're converting each digit to words with the help of for loop and switch case. Remember that the array contains the values in reverse order so we're also executing our for loop in reverse direction too.
Let's understand this program with an example. Suppose n = 123 then the steps involved are -
Working of While Loop -
n > 0 or 123 > 0. So while loop executes with c = 0 -
- a[c] = n % 10 or a[0] = 123 % 10 = 3.
- n = n / 10 = 123 / 10 = 12.
- c++ or c = 1.
n > 0 or 12 > 0. So while loop executes with c = 1 and a[0] = 3 -
- a[c] = n % 10 or a[1] = 12 % 10 = 2.
- n = n / 10 = 12 / 10 = 1.
- c++ or c = 2.
n > 0 or 1 > 0. So while loop executes with c = 2, a[0] = 3 and a[1] = 2 -
- a[c] = n % 10 or a[2] = 1 % 10 = 1.
- n = n / 10 = 1 / 10 = 0.
- c++ or c = 3.
Now n is not greater than zero so while loop terminates. Now c = 3, a[0] = 3, a[1] = 2 and a[2] = 1.
After while loop, for loop is executed. Our for loops runs for i = c - 1 to 0, decrementing value of i each time. Inside for loop we have a switch case condition in which we're passing the value of array a[10]. Take a look at the working of for loop.
Working of For loop -
i = c - 1 = 3 - 1 = 2 -
- We're passing a[i] in switch statement i.e. a[2] = 1 (first digit of our number).
- When 1 is passed as a value in switch statement the case marked with value 1 i.e. CASE 1 is executed, printing the text "ONE ".
- Next using break statement we're exiting the switch case and for loop is executed again with updated values.
i = 1 -
- We're passing a[i] in switch statement i.e. a[1] = 2 (second digit of our number).
- When 2 is passed as a value in switch statement the case marked with value 2 i.e. CASE 2 is executed, printing the text "TWO ".
- Next using break statement we're exiting the switch case and for loop is executed again with updated values.
i = 0 -
- We're passing a[i] in switch statement i.e. a[0] = 3 (third digit of our number).
- When 3 is passed as a value in switch statement the case marked with value 3 i.e. CASE 3 is executed, printing the text "THREE ".
- Next using break statement we're exiting the switch case and for loop is executed again with updated values.
i = -1 so for loop is terminated.
Tip: To understand the program more perfectly you can also take different values of n and try the above process yourself. It will help you to understand the process much clearly and helps in improving your debugging ability.
We have to display like for example 123 one hundred twenty three rupees
Hello There,
C Programming Tutorial being contrived to exist for many projects simply so it can be run will be the first to hit the wall, but those projects where the functions to make existing transactions cheaper in real world applications will find the elusive real world demand
I noticed that the expression x+++y is equal to x+(++y)
what make c to choose this and not (x++)+y?
Please keep providing such valuable information.
Grazie,
Lorris