In: Computer Science
In c++
Rewrite the following recursive method into an
iterative function.
void mystery (int n)
{
cout<<"? ";
if (n > 0)
mystery (n - 1);
}
Dear Student,
below i have written the iterative function as per the requirement, i have also written the complete C++ program along with the output for your bettr understanding.
=============================================================
Iterative Method:
void mystery (int n)
{
cout<<"? ";
while(n>0)
{
cout<<"? ";
n--;
}
}
===================================================================
Complete Program:
#include<iostream>
using namespace std;
void mystery (int n)
{
cout<<"? ";
while(n>0)
{
cout<<"? ";
n--;
}
}
int main()
{
mystery(5);
cout<<endl;
return 0;
}
============================================================
Sample Output:
=============================================================
Kindly Check and Verify Thanks..!!!