Write a recursive void function that has one parameter, which is a positive
integer. When called, the function writes its argument to the screen backward.
That is, if the argument is 1234, it outputs the following to the screen:
4321
Recursive function:
//Recursive function of backwardNums:
//It takes one parameter, which is a positive integer.
//When called, the function writes its argument to the screen backward.
void backwardNums(int number)
{
if (number < 10)
{
cout << number;
}
else
{
cout << (number%10);
backwardNums( number/10);
}
}
Complete program by using above recursive function:
//Header section
#include <iostream>
using namespace std;
//Function prototype
void backwardNums(int n);
//main program
int main( )
{
//Call the method
cout<<"If the argument is 1234,then the output: ";
backwardNums(1234);
return 0;
}
//Recursive function of backwardNums:
//It takes one parameter, which is a positive integer.
//When called, the function writes its argument to the screen backward.
void backwardNums(int number)
{
if (number < 10)
{
cout << number;
}
else
{
cout << (number%10);
backwardNums( number/10);
}
}
Output of the program: