Write a recursive void function that takes a single int argument n and
writes integers n, n-1, … , 3, 2, 1. Hint: Notice that you can get from the
code for Self-Test Exercise 4 to that for Self-Test Exercise 5 (or vice versa) by an exchange of as little as two lines.
Recursive function:
//Recursive function of decreaseCount:
//It takes a single int argument n and
//writes integers n, n-1, … , 3, 2, 1.
void decreaseCount( int n )
{
if (n >= 1)
{
cout <<n << " ";
decreaseCount(n - 1);
}
}
Complete program by using above recursive function:
//Header section
#include <iostream>
using namespace std;
//Function prototype
void decreaseCount(int n);
//main program
int main( )
{
//Call the method
cout<<"If the argument is 15,then the output: ";
decreaseCount(15);
return 0;
}
//Recursive function of decreaseCount:
//It takes a single int argument n and
//writes integers n, n-1, … , 3, 2, 1.
void decreaseCount( int n )
{
if (n >= 1)
{
cout <<n << " ";
decreaseCount(n - 1);
}
}
Output of the program: