recursive functions help

I am new to C++ and i am trying to create a recursive function that lists each individual digit in an integer and then provides the sum of those digits and ‘sum’ must be a variable that is passed to the function by reference. My program works but lists the digits in reverse order. How do i change it that the number are displayed in appropriate order? Any advice 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
#include <iostream>
using namespace std;

int digits(int& sum, int n) {
        if (n<10) return n;
        while (n!=0) {
                cout << n%10;
                sum +=n%10;
                n = n/10;
                cout << endl;
                }
        cout << sum;
        return digits (sum, n/10);
}

int main () {
        int s=0, n;
        cout << "Input a positive integer\n";
        cin >> n;
        if (n<=0) return 0;
        digits (s, n);
        cout << endl;
        return 0;
}
You are not very recursive. Recursive function can do this task without a loop.

The function should not print the sum. Leave that to the main().

Why do you return a value from the function?

"Works"? No, it does not. Give 7 as input.
Hint: Unless you are required to solve this using integers, or you are doing it for the challenge, using a string might be a better option.
> create a recursive function that lists each individual digit in an integer
> My program works but lists the digits in reverse order.

list each individual digit in an integer:

1
2
3
4
5
6
7
input: n, a positive integer

if n is a single digit number (less than ten), print out n and we are done

if n has two or more digits (greater than nine):
        list each individual digit in the integer n/10 (list every digit except the last digit)
        after that, print out the last digit n%10 

Topic archived. No new replies allowed.