Before start the program, see the recursion function of 'cheers' in Self-Test Exercise1. Now need to convert from recursive function to iterative function.
Iterative function of 'cheers ':
//Iterative version of the function 'cheers'
//Function "cheers" accept an integer parameter value of n
//and check the while loop condition to print result on screen
void cheers(int n)
{
while (n > 1)
{
cout << "Hip ";
n--;
}
cout << "Hurray\n";
}
Complete program by using above iterative function:
//Header section
#include <iostream>
using namespace std;
//Function prototype
void cheers(int n);
//main program
int main( )
{
//Call the method
cheers(3);
return 0;
}
//Iterative version of the function 'cheers'
//Function "cheers" accept an integer parameter value of n
//and check the while loop condition to print result on screen
void cheers(int n)
{
while (n > 1)
{
cout << "Hip ";
n--;
}
cout << "Hurray\n";
}
Output of the program: