#6 Trace the following program and show the output. #include <iostream.h> #inclu
ID: 3686746 • Letter: #
Question
#6 Trace the following program and show the output.
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void counting1(int);
void counting2(int);
void counting3(int);
void main()
{
int temp;
cout << "Enter a value for counting "; Enter 5
cin >> temp;
counting1(temp); // function call
cout << endl;
counting2(temp); // function call
cout << endl;
counting3(temp); // function call
}
void counting1(int n)
{ // Assume n is 5 to start with
if (n > 0)
{
cout << setw(5) << n;
counting1(n - 1);
cout << setw(5) << n;
}
else
cout << setw(5) << n;
}
void counting2(int m) // Assume m is 5 to start with
{
if (m >= 0)
{
cout << setw(5) << m;
counting2(m - 1);
};
}
Explanation / Answer
Some syntax correction needed in program, corrected version is given below with its output:
void counting2(int m) // Assume m is 5 to start with
{
if (m >= 0)
{
cout << setw(5) << m;
counting2(m - 1);
}
-----------------------------------------------------------------
OUTPUPT:
5 4 3 2 1 0 1 2 3 4 5
5 4 3 2 1 0
5 4 3 2 1 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.