Can someone explain how to get this output? How do I trace this?

Output:
*
**
***
****
*****
******
*******

#include <iostream>
using namespace std;

void Triangle(int x) {
   if (x <= 0) return;
   Triangle(x - 1);
   
   for (int i = 1; i <= x; i++) 
        cout << "*";
   cout << endl;
}

int main() {
   Triangle(7);
   return 0;
}

When the function is called from main x has a value of 7, and the function calls itself with a value of x - 1 until x equals 0. So therefore, the function is called 7 times with x = 1 - 7 and outputs the * based on the value of x.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.