473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

splitting numbers into separate digits

a c++ program to accept a value and split it digit by digit and display in name

eg:123
>one two three
Dec 24 '12 #1
4 3928
weaknessforcats
9,208 Expert Mod 8TB
So 123/100 is 1

Subtract 100 from 123 to get 23

23/10 is 2

Subtract 2* 10 from 23 to get 3.

That's the algorithm. All you have to now is code it up.
Dec 24 '12 #2
divideby0
131 128KB
another idea would be to read the value into a string, use an array to hold the written representation of each digit, and loop through the input.

Expand|Select|Wrap|Line Numbers
  1. string num_text[] = { "zero", "one", "two", ... };
  2.  
Expand|Select|Wrap|Line Numbers
  1. num_text[input.at(loop_index_pos) - '0'];
  2.  
Dec 24 '12 #3
whodgson
542 512MB
Another way which I think closely mirrors weaknessforcats algorithm is:
1.)get the number with cin>>
2.)mod(%) the number with 10 to get the least significant digit as the remainder and store it somewhere.
3.)divide the number by 10 to lop that digit.
4.)repeat 2.) and 3.) until digits exhausted
5.)your store is now full of individual digits.
6.)fill a container (array or vector) with the ten digit names e.g."zero","one",..."nine".
7.)set up a loop to match each digit with its name and print it.
Feb 5 '13 #4
donbock
2,426 Expert 2GB
This question comes up with some regularity. Take a look at...
how to print the numbers in words?
Print a number in words
Input numbers output words problem
c program to convert numbers between 1 and 99999
Converting numbers to spellings (eg, 10 to ten)
How to convert numerical currency value (33,163.33) into words?
digits to words

There are lots more. These are the first few that I found by searching for "number words hundred". The search bar is at the top-right corner of the Bytes page.
Feb 5 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tommy Lang | last post by:
Lets say you have a read (using cin >> ) a couple of numbers that the user have entered ie. 1234. And you want to add these numbers together like this, 1+2+3+4=10 How do you do that? Thanks, ...
2
by: Jason | last post by:
What I am trying to do is get all of the records in a table that are out of sequence so I know which account numbers I can reuse. I have a range of account numbers from 50100 to 70100. I need to...
96
by: Gustav Hållberg | last post by:
I tried finding a discussion around adding the possibility to have optional underscores inside numbers in Python. This is a popular option available in several "competing" scripting langauges, that...
11
by: MM | last post by:
Hi I have never written any C programs before, but it seems that I need to do so now. Hope some of you out there can spend a few minutes and help me by writing a simple example of something...
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
12
by: bejiz | last post by:
Hi, Do you know a way to tell to the compiler that if there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this: " ". But if I have numbers of 3 digits, I want spaces like this: " ...
8
by: Abedin | last post by:
I have 9 digits in form of "111020402". Then I have another 9 digits in form of "111020403". I have 100,000 records of these two 9-digit numbers. I want to filter this as follows: District:...
2
by: rickbill | last post by:
Dim iRand As Integer Dim iRand1 As Integer Dim iRand2 As Integer iRand = oRand.Next(1, 10) iRand1 = oRand.Next(1, 10) iRand2 = oRand.Next(1, 10) ...
0
by: Streen | last post by:
I am looking for a function in visial basic.net that takes a string, containing a decimal, like 12.5958, and returns the string with x numbers of digits behind the comma with appropriate rounding. ...
1
by: Andrea Robinson | last post by:
Hello! I need a little more help. I have managed to convert all my chars input from a text file into digits. Example: Input from file: $1,9,56#%34,9 !4.23#$4,983 Output: 1956 349 423 4983
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.