Write a program in c to accept a number and display the digits in words
Answers
Answered by
3
Program to print number in words
Update: As observed by one of our readers, the above programs fails to show correct output for any integer that ends with 0. Such as 1000, 1090, 10 etc. Therefore I have made little changes in the above program to remove the bug. I have also commented the program well enough so that it could be easy to get the logic.
Program to display number in words
Output
Enter any number to print in words: 1007
One Zero Zero Seven
Happy coding
Recommended readings
Loop programming exercises index.
C program to print alphabets from a to z .
C program to print multiplication table of any number.
C program to count number of digits in any number.
C program to find sum of first and last digit of any number.
C program to find frequency of each digit in given integer .
C program to count number of words in a given string .
About Pankaj
Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.
Follow on: Facebook | Twitter | Google | Website or View all posts by Pankaj
Have a doubt , write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>
/ **
* C program to print number in words
* /
# include < stdio . h >
int main( )
{
int n , num = 0 ;
/ * Input number from user * /
printf( " Enter any number to print
scanf( " % d " , & n ) ;
/ * Store reverse of n in num * /
while( n != 0 )
{
num = ( num * 10 ) + ( n % 10 ) ;
n / = 10 ;
}
/ *
* Extract last digit of number an
* till num becomes 0
* /
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 ;
**
* C program to display number in word
* /
# include < stdio . h >
# include < math . h >
int main( )
{
int n , num = 0 , digits ;
/ * Input number from user * /
printf( " Enter any number to print
scanf( " % d " , & n ) ;
/ * Find total digits in n * /
digits = ( int ) log 10 ( n ) ;
/ * Store reverse of n in num * /
while( n != 0 )
{
num = ( num * 10 ) + ( n % 10 ) ;
n / = 10 ;
}
/ * Find total trailing zeros * /
digits = digits - ( ( int ) log 10 ( nu
/ *
* Extract last digit of number an
* till num becomes 0
* /
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 ;
Update: As observed by one of our readers, the above programs fails to show correct output for any integer that ends with 0. Such as 1000, 1090, 10 etc. Therefore I have made little changes in the above program to remove the bug. I have also commented the program well enough so that it could be easy to get the logic.
Program to display number in words
Output
Enter any number to print in words: 1007
One Zero Zero Seven
Happy coding
Recommended readings
Loop programming exercises index.
C program to print alphabets from a to z .
C program to print multiplication table of any number.
C program to count number of digits in any number.
C program to find sum of first and last digit of any number.
C program to find frequency of each digit in given integer .
C program to count number of words in a given string .
About Pankaj
Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.
Follow on: Facebook | Twitter | Google | Website or View all posts by Pankaj
Have a doubt , write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>
/ **
* C program to print number in words
* /
# include < stdio . h >
int main( )
{
int n , num = 0 ;
/ * Input number from user * /
printf( " Enter any number to print
scanf( " % d " , & n ) ;
/ * Store reverse of n in num * /
while( n != 0 )
{
num = ( num * 10 ) + ( n % 10 ) ;
n / = 10 ;
}
/ *
* Extract last digit of number an
* till num becomes 0
* /
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 ;
**
* C program to display number in word
* /
# include < stdio . h >
# include < math . h >
int main( )
{
int n , num = 0 , digits ;
/ * Input number from user * /
printf( " Enter any number to print
scanf( " % d " , & n ) ;
/ * Find total digits in n * /
digits = ( int ) log 10 ( n ) ;
/ * Store reverse of n in num * /
while( n != 0 )
{
num = ( num * 10 ) + ( n % 10 ) ;
n / = 10 ;
}
/ * Find total trailing zeros * /
digits = digits - ( ( int ) log 10 ( nu
/ *
* Extract last digit of number an
* till num becomes 0
* /
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 ;
amitkuntal15:
answer milna chaiya bsss vo mil gya
Similar questions
Science,
7 months ago
Computer Science,
1 year ago
Computer Science,
1 year ago
Math,
1 year ago
Economy,
1 year ago