Sum of Digits?

So, I'm wondering how one goes about taking an answer or result, and manipulating the individual digit data, for example:

98 + 1 = 99

and taking the answer 99 and braking it into:

9 + 9 = 18

then

1 + 8 = 9 and so on.

I get I would have to loop the process until the result is < 10, but how do I store the answer into individual digits? It really seems to escape me. Also, some of the answers would be floats and/or have an undetermined length until calculated.

If i'm not mistaken, if I try to put it into an array it would just store the whole number (i.e i[0] would store "99", instead of i[0] = 9 and i[1] = 9). Or did I miss O_o
You might have more help if you set this to a qustion
closed account (D80DSL3A)
For integers this is easy. Use division. The / gives the quotient and % gives the remainder.
Example:
75/10 = 7 and 75%10 = 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main () {

	int n;
	int prvi;
	int vtori;
	int suma;

	cout<<"Enter number: ";
	cin>>n;

	prvi = n/10;

	cout<<"First number: "<<prvi<<endl;

	vtori = n%10;

	cout<<"Last number: "<<vtori<<endl;

	suma = prvi + vtori;
	cout<<"The sum: "<<suma<<endl;

	cin.get(); cin.get();
	return 0;


Thiss will help ;)
what about in string functions...
is it possible in string?
Well, lets try this with an example run of what i'm doing.

The equation itself is (xy/z)n^2y^2) = a. Sum the digits of "a"

or
x | y | z | x^2 | y^2 | a
1 * 2 / 3 * 1^2 * y^2 = 2.6

then
(ignoring the decimal point)
2 + 6 = 8

as for ty98, it has multiple questions in it in fact, and I would rather you said something useful, as opposed to a statement that doesn't refer to the [multiple] questions in any way, shape, nor form.


Janlan, your method works like a charm, but how do I apply it to numbers that have decimal places? Or some way to remove the decimal place altogether, since the "places" of the numbers do not matter once the equation is run. I.E. it doesn't matter if its 2.6, .26, 26., I just need to extract the digits.

Also, it needs to scale to the number size itself, as the answers could be anywhere in length from 1 to millions of digits long with decimals and without D=>

For example, the first run could give me an answer of 36, where the second run could give me an answer of 30023123764533.654639.

[update]
Later Update: I think this: http://www.cplusplus.com/reference/std/locale/numpunct/grouping/ is what i'm aiming for? trying it out to see, will post results in-case anyone else is travelling this winding road :P

[update]
Looks like it takes a string value and seperates it into sets of n characters... Not quite what i'm looking for but getting closer.

[update]
Good lord, after a bit more research i've noticed that "Sum of Digits" math, something that is just a curiosity, has Number Theory conferences and symposiums dedicated to it.... Perhaps this is out of my league.

[update]
And bingo. A couple of years ago the answer was posted:
http://www.cplusplus.com/forum/beginner/4988/
Now to see about the random size + decimal problem, though it looks like this might solve that too.

:P that only took 6 hours of research ^_^.
Doing final tests before changing to [solved]
Last edited on
galameth wrote:
Or some way to remove the decimal place altogether

http://stackoverflow.com/questions/3882304/replace-remove-character-in-string

get input as string*->remove '.'->use stringstream to convert it to integers->use Janlan's suggestion(?)

*might like this article: http://www.cplusplus.com/forum/articles/6046/
Last edited on
So i'm going brain dead. My sum of digits part works fine, until it gets to any number iteration that = 10. For the life of me I can't wrap my head around how to get it to run through it twice, or as many times as needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <iostream>

using namespace std;

int main()
{

int num;
int a = 0;
int sum = 0;
int fin = 0;
//ask for a number
cout << " Enter a number : ";
cin >> num;

//as long as that number is greater then 0, run
while ( num > 0 ) {
sum += num % 10;
num /= 10;
}

//output result.
cout << sum;
cout << endl;

//the above portion runs perfectly, except if the number produces a 
//result greater then 9, in which case it gives a proper answer but
//isn't what is needed. I.E. the number 19 sums into "10". instead
//of continuing to run and reduce "10" into "1" it outputs and continues along

//attempting to apply the method to "sum" results in
//the program inf looping and stalling.
if ( sum >= 10 ) {
while ( sum > 0 )
{
    sum += a % 10;
    a /= 10;
}
}
cout << "Second";
cout << endl;
cout << a;


So what on earth am I missing? o.o
1
2
3
4
5
while ( sum > 0 )
{
    sum += a % 10;// a is zero, and sum is unchanged
    a /= 10;// a is still zero
}


Do you mean:
1
2
a += sum%10;
sum /= 10


You might want a loop though. What if num == 98789789?
Ha! Awesome, Stupid me I had "a" and "sum" backwards. Thank you kindly for that catch.

Otherwise, thats what started the mess to begin with, is trying to figure out how to cause it to catch if the answer, when all said and done, is STILL > 9, and if so, run it through it again. but i'm thinking i could just have this lot a separate function and have it called through another loop until sum <= 9?
but i'm thinking i could just have this lot a separate function and have it called through another loop until sum <= 9?


Yup. That's how I'd do it too.
Topic archived. No new replies allowed.