I understand that this is a very trivial problem.
I am trying to write a program that will keep asking for user input as an int until the user enters 0 at which point it will terminate and print out all the digits in reverse. This is what I have so far:
int revList(int num)
{
if (num != 0)
{
scanf("%d", &num);
printf("%d\n", revList(num));
return num;
}
return 0;
}
with my main method being:
int revList(int);
printf("%d", revList(1));
the 1 in the revList
was chosen arbitrarily to satisfy the compiler's need for an arguement even though it's useless in the first iteration.
I understand that my code is probably inefficient and not making use of recursion properly.
The code works but has 1 problem: it prints 0 two times and then the preceding numbers. I realize where the problem stems from. It's because I have a return 0 at the end but I can't take that out because it will reach the end of a non-void function and in this manner, it returns 0 twice.
Can someone please help me fix this and give me an efficient method to address this problem?
EDIT: example: user inputs: 1 2 3 4 5 8 0
it should output:
0 8 5 4 3 2 1
my program will output: 0 0 8 5 4 3 2 1