Trouble with program

Pages: 12
closed account (yR9wb7Xj)
Write a program that prompts the usr to input an integer and then output both the individual digits of the number and the sum of the digits. For example, it should output the following digits 3456 as 3 4 5 6, 8030 as 8 0 3 0, 2345526 as 2 3 4 5 5 2 6, 4000 as 4 0 0 0, and -2345 as 2 3 4 5. (I've tried so many of times and cannot get a solution for it. This is just for me, I'm trying to understand how to solve these logical problems, but I seem to have a hard time with it.)
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{

int digits, sum;

cout << "Enter individual digits between 0 & 10." << endl;
cin >> digits;


if(digits < 10)
		sum+=digits;
	if (digits < 0)
		sum -=digits;

while(digits !=sum)
{
cout << "You've enter a digit larger then 10." << endl;
}
cout << " : " << digits << setw(2) << sum << endl;


	return 0;
}
Last edited on
One quick way I could think of is this :

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string digits;
	int sum = 0;

	cout << "Enter integer." << endl;
	cin >> digits;

	for (int i = 0; i < digits.length(); i++)
	{
		if (digits[i] == '-') continue; //ignore minus
		cout << digits[i] << " ";
		sum += digits[i] - '0';
	}
	cout << endl;
	cout << "The sum of individual digits is : " << sum << endl;

	system("pause");

	return 0;
}
closed account (yR9wb7Xj)
Thank you for this solution, unfortunately I cannot used this solution because I have not learned arrays yet. inside your for loop block in the if statement you're declaring digits[i] which is an array. In the chapter that I've just learned it's only nested loops or loops. I know what an array is but in this chapter I'm trying to solve the problems from what I've learned.
Last edited on
closed account (yR9wb7Xj)
I've tried it a different way using dowhile and I still can't get the correct solution, at this point I would consider myself a loser because I cannot figure this out.
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {

	int digits, sum;


	do{
		cout << "Enter integers." << endl;
		cin >> digits;
		if(digits <10)
		{
			sum += digits;

		}
		if( digits < 0)
		{
			sum+=digits;
		}


	}while(false);

	cout << digits << sum;







return 0;
}

Last edited on
closed account (yR9wb7Xj)
I still have not figure out the solution for this problem, can someone please help me out.
Ok let's go through some things here. First of all you are using the variable digits but you haven't given it any value yet. So digits>10 inside your if statement is wrong. Even if you did initialized though what values is it meant to hold?
closed account (yR9wb7Xj)
I just edited do while loop and I saw what you meant, it wasn't initializing to anything, so I fixed it. So any whole digits less than 10 will be outputted.
Last edited on
closed account (yR9wb7Xj)
What does this have to do with my program?
Hes a bot who is spamming.. eventually he will be banned (hopefully)
closed account (yR9wb7Xj)
Yeah I just reported him, thanks for letting me know.

Ok, lets look at an example here, it doesnt completely write the whole thing for you but will give you a good starting point and a better understanding on how to break down the digits.. lets say I enter 1001 as a integer and store it in Num.

We first create a loop to loop around until Num hits zero.

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
So, inside my loop...

I first do modulus with 10 (1001 % 10) which gives gives a result of 1  (my first digit)
I then divide my number by 10 giving 100
I add my modulus result (1 so far) to my total

Loop again as Num is 100 and still over 0

I do modulus with 10 (100 % 10) which gives gives a result of 0  (my second digit)
I then divide my number by 10 giving 10
I add my modulus result (0) to my total

Loop again as Num is 10 and still over 0

I do modulus with 10 (10 % 10) which gives gives a result of 0  (my third digit)
I then divide my number by 10 giving 1
I add my modulus result (0) to my total

Loop again as Num is 10 and still over 0

I do modulus 10 which gives gives a result of 1  (my fourth digit)
I then divide my number by 10 giving 0
I add my modulus result (1) to my total

Num is now 0 so we break from loop.  


Total now would hold the sum of digits.

A bit of pseudo:

1
2
3
4
5
6
7
Program Digits
Read Integer into Num
While Num > 0
	Digit = Num % 10
	Num = Num / 10
	Total = Total + Digit
End While


And some code :)

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

#include <iostream>

int main()
{

	int myNum;	// holds number I input
	int total = 0;  // holds sum of digits.
	int digit; // temp to hold each digit

	std::cout << "Enter Integer: ";
	std::cin >> myNum;

	while (myNum>0)
	{
		digit = myNum % 10;
		myNum /= 10;
		total += digit;
	}

	std::cout << std::endl << "Sum of digits is: " << total;

	return 0;

}

Enter Integer: 1001

Sum of digits is: 2

closed account (yR9wb7Xj)
Is this correct? Thank you for the reply and explaining how to break down the digits, it made sense, I just edit your code by adding another while loop if I did it correct, and I do not take credit for this because this your work, so thank you for explaining to me. I think I'm going to take a break from solving problems and really take my time on learning the syntax and understand the concept of them. But again, thank you, you're awesome!
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
#include <iostream>
using namespace std;
int main()
{
	int myNum;
	int total = 0;
	int digit;

	cout << "Enter an integer" << endl;
	cin >> myNum;

	while(myNum >0)
	{

		digit =myNum %10;
		myNum/=10;
		total += digit;



	}
	while (myNum < 0){
		digit =myNum %10;
		myNum /=10;
		total +=digit;

	}
	cout << "The sum of digit is:" << total << endl;








	return 0;
}
Last edited on

You don't need the second while loop, the one I provided performs the calculation of digits.
closed account (yR9wb7Xj)
Oh, what would I need to add the total of a negative number? Because it said in the problem we need to output the sum of the -2345. So I thought if I created another loop it would be fine because it did that. I think I rush myself working on the problems without learning the concept of the syntax, because I just finish taking my program 1 class this semester, and I did not learn a thing in the class, but manage to finish with a decent grade a B. So it's my 2nd week on learning C++ on my own I start it all over. And I thought I was ready to solve problems but I guess I'm not.
Last edited on
^
Turn it to positive by using absolute value?
Although does he want the first integer to remain negative?

If you were to turn them all into positive and add them you would get
2+3+4+5 = 14
If the first integer remains negative, you would get
-2 + 3 + 4 + 5 =10

Does the prompt state which is the correct case?
closed account (yR9wb7Xj)
I think it was the total of the -2345 to equal a negative number of that output. But I was thinking the same it could be the 2nd option. I'm not sure though in the practice problem from the book it did not specify what they are looking for after output -2345.

Sorry I see why you added the second while loop.. had a memory blank earlier and forgot it needed to support negative numbers. I would think the negative would be ignored and the digits added together in the normal way, but I may be wrong.. depends what he wants.

closed account (yR9wb7Xj)
I'm not sure want it's really asking for but I think it's asking for the outcome of -2345 to be a negative result of that outcome.
Last edited on

So if you entered -1001, you expect -2? - seems a little odd to me, or as Momothegreat said is it to take into account the first digit being -1 giving result of 1

If you expect -2 then your while loop would return that.
Last edited on
closed account (yR9wb7Xj)
I think you're right because -1001 should be 0 and not -2, but then again if -1001 is negative why wouldn't the other digits after after -1 would not be negative? For example if you take the sum of two negative digits shouldn't it result as a negative? Like -1-1=-2 or in this -1 + (-1) =-2
Pages: 12