Sun of digits in a number

J

Jay Nabonne

Thanks for the obvious.
Anyways this is what I got so far, however for some reason only the
last digit displays.
while (dig > 0) {
dig = dig%10; // (1)
total += dig%10; // (2)
dig = dig/10; // (3)
}

Just about. Just don't do the initial mod in the loop (1). You want to add
the mod to the total (2), and then divide by 10 to get the next digit into
low position (3).

- Jay
 
M

Mike Wahler

Hello, I would like to know how to do this:
Basically when I input a number, such as 123, it should display 6
(1+2+3), and for 666 would be 18. I'm a bit stuck here...

I know it would involve a while loop along that can do this condition:

nd1=num/100%10;
nd2=num/10%10;
nd3=num%10;, etc

And some how add it up. Can anyone help me? Thanks.

#include <iostream>
#include <numeric>
#include <sstream>
#include <string>

unsigned int sum_digits(int num)
{
std::eek:stringstream oss;
oss << num;
const std::string s(oss.str());
return std::accumulate(s.begin(), s.end(), 0) - '0' * s.size();
}

int main()
{
std::cout << sum_digits(123) << '\n';
return 0;
}

:) :) :)

-Mike
 
W

wizofaus

C'mon, why would you want such a convoluted solution.

It's surely far simpler:

#include <numeric>

struct digit_iterator
{
digit_iterator(int num = 0) : _num(num) { }
int operator* () { return _num % 10; }
digit_iterator& operator++() { _num /= 10; return *this; }
bool operator!=(const digit_iterator& i) { return _num != i._num; }
private: int _num;
};

int sum_digits(int num)
{
return std::accumulate(digit_iterator(num), digit_iterator(), 0);
}

(and yes I'm sure someone will point that digit_iterator isn't a fully
valid input iterator).
 
G

Greg

C'mon, why would you want such a convoluted solution.

It's surely far simpler:

#include <numeric>

struct digit_iterator
{
digit_iterator(int num = 0) : _num(num) { }
int operator* () { return _num % 10; }
digit_iterator& operator++() { _num /= 10; return *this; }
bool operator!=(const digit_iterator& i) { return _num != i._num; }
private: int _num;
};

int sum_digits(int num)
{
return std::accumulate(digit_iterator(num), digit_iterator(), 0);
}

(and yes I'm sure someone will point that digit_iterator isn't a fully
valid input iterator).

A template metaprogram is the way to go here:

#include <iostream>

template <unsigned int I>
int PrintDigits()
{
PrintDigits<I/10>();
std::cout << I%10;
}

template <>
int PrintDigits<0>() {}

int main()
{
PrintDigits<12345>();
std::cout << std::endl;
}

Greg
 
G

Greg

C'mon, why would you want such a convoluted solution.

It's surely far simpler:

#include <numeric>

struct digit_iterator
{
digit_iterator(int num = 0) : _num(num) { }
int operator* () { return _num % 10; }
digit_iterator& operator++() { _num /= 10; return *this; }
bool operator!=(const digit_iterator& i) { return _num != i._num; }
private: int _num;
};

int sum_digits(int num)
{
return std::accumulate(digit_iterator(num), digit_iterator(), 0);
}

(and yes I'm sure someone will point that digit_iterator isn't a fully
valid input iterator).

A template metaprogram is the way to go here:

#include <iostream>

template <unsigned int I>
int PrintDigits()
{
PrintDigits<I/10>();
std::cout << I%10;
}

template <>
int PrintDigits<0>() {}

int main()
{
PrintDigits<12345>();
std::cout << std::endl;
}

Greg
 
W

wizofaus

Greg said:
A template metaprogram is the way to go here:

#include <iostream>

template <unsigned int I>
int PrintDigits()
{
PrintDigits<I/10>();
std::cout << I%10;
}

template <>
int PrintDigits<0>() {}

int main()
{
PrintDigits<12345>();
std::cout << std::endl;
}
Um...I assume you actually meant...

template <unsigned int I>
int SumDigits()
{
return I % 10 + SumDigits<I/10>();
}

template <>
int SumDigits<0>() { return 0; }


The slight inconvenience that the program needs to be recompiled for
every different number is surely offset by the blindingly fast run-time
efficiency.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top