Answer the question
In order to leave comments, you need to log in
How to print numbers in reverse order without using arrays in c++?
Please help me decide, and most importantly, understand how to solve this task:
The standard input of the program is a sequence of space-separated positive integers ending in zero. It is required to print the same sequence in reverse order (without zero), separating the numbers with spaces. It is forbidden to use arrays (even if you are already familiar with them). (Hint: use recursion).
Sample Input :
1 2 3 4 0
Sample Output :
4 3 2 1
To begin with, I decided to try to implement at least the output of numbers in a given order. It just happened like this:
#include <iostream>
using namespace std;
int numb(int x){
if (x != 0){
cout << x << " ";
}
return 0;
}
int main()
{
int numbers = 0;
while (cin >> numbers){
numb(numbers);
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
press ctrl+F5 in studio, type for example "1 2 3 0" without quotes and hit enter ;)
#include <iostream>
void foo()
{
int number;
cin >> number;
if (number != 0)
{
//фишка в том что:
foo(); //сначала выполняется функция
cout << number << " "; // а после уже выполняется вывод
}
}
int main(int argc, char *argv[])
{
foo();
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question