C++ Outputting individual digits and the sum(Correct order!)

Assignment for computer engineering, I get extra credit if I print the numbers out from left to right instead of right to left. You're asked to have the user input an integer and then output both the individual digits and the sum of those digits.
Example: User inputs 3456
Output: 3 4 5 6 , Sum = 18

What I have so far, I've tried quite a few different things and can't gget it right, I'm missing something simple. Any help would be appreciated
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int num, count, sum;
	char A;

	cout << "Please enter a whole number" << endl;
	cin >> num;
	   
	do
	{
		A = num % 10;
		num = num / 10;
		A++;
	}

	while (num / 10 == 0);

	sum = A + A++;

	cout << "The digits are: " << A << " " << ++A << endl;
	cout << "The sum of those digits = " << sum << endl;

	return 0;
}
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int num, count, sum, limit;
	int A;

	cout << "Please enter how many digits are in your set" << endl;
	cin >> limit;
	cout << "Please enter a whole number" << endl;
	cin >> num;

	count = 0;
	sum = 0;

	do
	{
		A = num % 10;
		num = num / 10;
		cout << "The digits are: " << A << endl;
		count++;
		sum = sum + A;
	}

	while (count < limit);



	cout << "The sum of those digits = " << sum << endl;

	return 0;
}

New code, outputs the digits in reverse order, correct sum
Last edited on
Topic archived. No new replies allowed.