Printing each digit of an integer

Task:
1. In main() funtion, ask user to enter some positive number. If number is not positive, terminate the program.
2. Otherwise you call a seperate function from main() that takes number and:
-prints digits of this number;
-returns/prints the sum of digits

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
#include <iostream>
using namespace std;

int both(int n) {

    int remainder;
    int sum = 0;

    while (n>0) {
        remainder = n%10;
        sum = sum + remainder;
        int digit = n%10;
        n = n/10;
        cout << "The digits are: " << digit << endl;
        }
    return sum;
}

int main () {

    int x;
    cout <<"Enter a number: " << endl;
    cin >> x;
        if (x<=0){
            return 0;
        }
    cout << "The sum of digits is: " << both(x) <<endl;



return 0;

}


The program should run like this:
1
2
3
4
5
6
7
8
Enter a number:
345
The digits are:
3
4
5
The sum of digits is:
12


The only part I am having trouble with is printing the digits of an integer because from my code, it prints in REVERSED order instead of ascending order. I need to use a while loop inside my function. Please help, thanks in advance!
Last edited on
you could find how many digits you have in the first place and then work your way left to right. 1111 -> 4 digits. 1111%1000, 111 % 100 ... etc
Either that or place into an array your results and print that array in reverse.
I'm sure you'll get more answers soon :)
To get the first digit
1
2
3
4
5
6
7
8
unsigned int leftmost_digit(unsigned int number)
{
    int divisor=1;
    int i=0;
    for(i=number;i>9;i/=10)
        divisor*=10;
    return i;
}

To get the remainder
1
2
3
4
5
6
7
8
unsigned int remainder( unsigned int number)
{
    int divisor=1;
    int i=0;
    for(i=number;i>9;i/=10)
        divisor*=10;
    return number%divisor;
}
> it prints in REVERSED

Print a digit after the more significant digits have been printed.

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

int primt_and_sum_digits( int number )
{
    if( number == 0 ) return 0 ;

    const int least_significant_digit = number % 10 ; // print this at the end
                         // after the more significant digits have been printed

    const int sum = primt_and_sum_digits( number / 10 ) + least_significant_digit ;

    std::cout << least_significant_digit << ", " ;

    return sum ;
}

int main()
{
    for( int v : { 12, 345, 6789, 12345678 } ) // invariant: v is positive
    {
        std::cout << "The number is " << v << ",  its digits are " ;
        const int sum = primt_and_sum_digits(v) ;
        std::cout << "  and their sum is " << sum << ".\n" ;
    }
}

http://coliru.stacked-crooked.com/a/726a9c1140fa1610
keep it simple just use a string

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
#include <iostream>
#include <string>
using namespace std;

void print(string num);
int main()
{
	string num;

	cout << "Please enter a number: " << flush;
	getline(cin, num);

	if (num.empty() || num.front() == '-')
		return EXIT_FAILURE;
	
	print(num);

	cin.ignore(INT_MAX, '\n');
	return EXIT_SUCCESS;
}

void print(string num)
{
	cout << num;
}


the above code doesn't provide any error checking and is only meant as an example
Last edited on
I'm supposed to use a while loop to do this though. Is this possible? :(
Use an actual stack.

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 <stack>

int primt_and_sum_digits( int number )
{
    std::stack<int> digits ;
    int sum = 0 ;

    while( number != 0 )
    {
        const int least_significant_digit = number % 10 ;

        digits.push( least_significant_digit ) ; 
        sum += least_significant_digit ;

        number /= 10 ;
    }

    // print digits from top of the stack (reverse order of push) till it is empty
    while( !digits.empty() )
    {
        std::cout << digits.top() << ", " ;
        digits.pop() ;
    }

    return sum ;
}

int main()
{
    for( int v : { 12, 345, 6789, 12345678 } ) // invariant: v is positive
    {
        std::cout << "The number is " << v << ",  its digits are " ;
        const int sum = primt_and_sum_digits(v) ;
        std::cout << "  and their sum is " << sum << ".\n" ;
    }
}

http://coliru.stacked-crooked.com/a/4ac1be1cb0848f95
well you know how to reverse the number. If you reverse the number twice you get the original number.

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
44
45
46
#include <iostream>
using namespace std;

int both(int n) {

	int remainder;
	int sum = 0;

	while (n > 0)
	{
		sum = (sum * 10) + n % 10;
		n = n / 10;
	}

	n = sum;
	sum = 0;

	while (n>0) {
		remainder = n % 10;
		sum = sum + remainder;
		int digit = n % 10;
		n = n / 10;
		cout << "The digits are: " << digit << endl;
	}

	
	return sum;
}

int main() {

	int x;
	cout << "Enter a number: " << endl;
	cin >> x;
	cin.ignore();

	if (x <= 0){
		return 0;
	}
	cout << "The sum of digits is: " << both(x) << endl;


	cin.ignore();
	return 0;

}
The lazy approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>


int main()
{
    const int value = 123456789;
    std::stringstream ss;
    ss << value;
    const std::string strValue = ss.str();
    
    auto it = strValue.begin(); 
    int sum = 0;
    
    std::cout << "The digits are: ";
    while(it != strValue.end())
    {
        std::cout << *it << ' ';
        sum += *it - '0';
        ++it;
    }
    std::cout << std::endl << "The sum is: " << sum << std::endl;
}


http://coliru.stacked-crooked.com/a/1bd42a661e2b65a3

Also your professor limiting to a while loop is pointless. Anything you can do in a for loop you can do in a while loop and vice versa.
Thanks to everyone for replying, especially Yanson! I now understand what I was doing wrong.
Topic archived. No new replies allowed.