You are here:

C/how can i extract them? ------------------------- ...

Advertisement


Question
how can i extract them?


-------------------------
Followup To
Question -




How can i program in C so that somone inputs a number and it displays the number in words?

ie.

Input Number : 510

Output Five Hundred and Ten
Answer -
Extract the digits and not their place value and then print that.
Same way as you mentally read a number.

-ssnkumar

Answer
Use % operator to extract the digits.
For example, take the number 4582.
4582 % 10 = 2 (Digit# 1)
4582 / 10 = 458
458 % 10  = 8 (Digit# 2)
458 / 10  = 45
45 % 10   = 5 (Digit# 3)
45 / 10   = 4
4 % 10    = 4 (Digit# 4)

Since there are only 4 digits, it makes a thousand.
So, the last digit (Digit# 4) tells that it is "Four Thousand".
The previous digit is of a hundred. So, Digit# 3 tells that it is "Five Hundred".
Previous to that is of a Ten. So, Digit# 2 tells that it is "Eighty".
The last digit is "Two".
Combine all these and you will get:
4582 = "Four Thousand Five Hundred and Eighty Two".

Hope this helps.....

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.