Assuming that we enter the integer value is 5689798. Summing up the digits gives 5+6+8+9+7+9+8+8 = 58 that contains more than one digit, so we repeat the process: 5+8 = 13. we repeat until we got one digit: 1+3 = 4 . The final answer is 4.

I don't know how to isolate each digit in order to add them together.
Can anyone help me

Recommended Answers

All 18 Replies

I would investigate the modulus operator.

>>5+6+8+9+7+9+8+8 = 58
Wrong. you have 8 listed in both the last two digits. Should only be 5+6+8+9+7+9+8=52. 5+2=7.

At any event you need a loop that uses the mod operator % (e.g. num % 10) to extract the digit and divide operator / to remove it from the number in preparation for the next loop iteration.

You can use some math magic. Or you can get the input as a string, which is much easier.

For example here is some not tested code. It gets the input as a string. Then
sums the digits inside the string. And displays it.

string digits;
cin >> digits;
int sum = std::accumulate(digits.begin(),digits.end()) - digits.size() * '0';
for(int i = 0; i != digits.size() - 1; ++i){
 cout << digits[i] <<" + ";
}
cout << digits[digits.size()-1] << " = " << sum << endl;

I would use modulus and divide, this way is hard coded for a cretain max number size

something to the effect of the following

int sumDigitsUntillUnder10(int number)
{
 //check number islarger than 9
 int temp = number;
 int result = 0;
 while(temp>9)
 { //need to do some work
  //add each digit
  result  += temp%10;
  result  += temp/10;
  result  += temp/100;
  result  += temp/1000;
  result  += temp/10000;
  result  += temp/100000; 
  result  += ntemp/1000000;
  temp = result; //update value of temp
 }
//edit: forgot my return here ,... darn you vodka 
 return(temp);
}

Thats what i would do, but i cant say its the best way

Well, you would be doing it wrong. There is no need to hard code the number of digits.

int sumdigits(int n)
{
  int sum = 0;
  while( n > 0)
  {
    sum += (n%10);
    n /= 10;
  }
  return sum;
}

This may work..

int sumofDigits
{ 
 string str;
 int result = 0;
 cin>>str;
    for(int i=0; i<str.length(); i++)
      { 
    result += temp/pow(10,i);
       }
 cout<<"The result of "<<str<<" is = "<<result<<endl;
}

>>This may work..

Nope.

You really should compile and test the code you post that is supposed to solve, partially or otherwise, a problem.

Your code assumes all the bits are 1, which will usually not be the case.

And what's that temp variable? Its not declared anywhere in your code.

line 1 of the code you posted does not declare a functon.

Well, you would be doing it wrong. There is no need to hard code the number of digits.

int sumdigits(int n)
{
  int sum = 0;
  while( n > 0)
  {
    sum += (n%10);
    n /= 10;
  }
  return sum;
}

This program doesn't quite solve the problem, because the OP specified that if the result has more than one digit, you're supposed to sum the digits again; and so on until you reach a single digit.

Here's a solution that I think has the desired effect, although perhaps not quite by the means you might expect. I've also taken the liberty of making the argument and result unsigned, because I don't know what's supposed to happen if the argument is negative:

unsigned sumdigits(unsigned n)
{
    if (n == 0)
        return 0;
    return (n-1) % 9 + 1;
}
commented: Very nice and efficient +33

>Here's a solution that I think has the desired effect,
>although perhaps not quite by the means you might expect.

Clever. Though I'm curious how you came up with that solution. Was it intentionally designed from the start, or did you simply notice a pattern while working out a more brute force summation?

Clever. Though I'm curious how you came up with that solution. Was it intentionally designed from the start, or did you simply notice a pattern while working out a more brute force summation?

Actually, I first encountered the idea when my father taught it to me when I was a child. It's the basis for a technique called "casting out 9's" that I doubt is used much any more, but is a good way of checking arithmetic when you do it by hand.

For example, suppose you add 1247 and 2056 and get 3293. To check the result by casting out 9's, you add the digits of 1247 to get 14, and add them again to get 5. Then you add the digits of 2056 to get 13 and add them again to get 4. 5 + 4 is 9, so you should also be able to get 9 by adding the digits of 3293. However, when you try, you will see that you get 17 and then 8, so the addition is incorrect.

This technique works because modulus distributes over addition and multiplication. In other words, (a + b) mod n is always equal to (a mod n + b mod n) mod n, and similarly for multiplication.

Now consider a 2-digit decimal number ab. This number represents (a*10) + b. Notice that 10 mod 9 is 1. Distributivity implies that (a * 10) mod 9 is equal to ((a mod 9) * (10 mod 9)) mod 9. Becayse 10 mod 9 is 1, ((a mod 9) * (10 mod 9)) mod 9 is equal to a mod 9.

Similarly, ((a * 10) + b) mod 9 is equal to (a mod 9 + b mod 9) mod 9, which is equal to (a + b) mod 9. So we have just proven for a 2-digit number that the number mod 9 is equal to the sum of its digits mod 9.

commented: Well explained. +22

>>This program doesn't quite solve the problem
I didn't intend for it to be a complete solution. We are not supposed to provide complete solutions to home work problems.

I like your solution :)

>>This program doesn't quite solve the problem
I didn't intend for it to be a complete solution. We are not supposed to provide complete solutions to home work problems.

I meant that it gives a different answer in some cases than the problem requested.

(I suspect that we are working at a similar goal in different ways--because my suggestion essentially solves a different problem that happens to have the same solution as the one originally requested :) )

The code I posted needs to be called repeadedly until it returns a single digit value. But your solution is a lot better and less time consuming. But I don't know if it is something that a student would want to turn in as the solution to the problem because most likely the teacher would ask him to explain it, which he may or may not be able to do.

The code I posted needs to be called repeadedly until it returns a single digit value. But your solution is a lot better and less time consuming. But I don't know if it is something that a student would want to turn in as the solution to the problem because most likely the teacher would ask him to explain it, which he may or may not be able to do.

Exactly. That's why I posted it. It solves the problem, but not in a way that the student can use without first understanding and rewriting it.

#include<iostream>
using namespace std;
int sumdigits(int x)
{
  int total = 0;
  while(x>0)
  {
    total+=(x%10);
    x/= 10;
  }
  return total;
}
 
int main()
{
	int number=0;
	cout<<"Enter a number";	cin>>number;
	int result=sumdigits(number);
	if(result<10)
	{
		cout<<"Total is: "<<result<<endl;
		exit(0);
	}
	while(result>9)
	{
		result=sumdigits(result);
	}
	cout<<"Total is: "<<result<<endl;
	return 0;
}

use modulos operator or to isolate each digit

use modulos operator or to isolate each digit

I did that in sumdigits(). The rest are just repeated function calls.

#include<iostream.h>
#include<conio.h>
int main()
{
    long int x,y, sum=0;
    cin>>x;
    while(x>9)
    {
         while(x!=0)
         {
                    y=x%10;
                    x=x/10;
                    sum=sum+y;
                    }
         if(x==0)
         {
                 x=sum;
                 sum=0;
                 }
         }
    cout<<x<<endl; 
    system("PAUSE");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.