mardi 2 août 2016

How do print the following pattern in C/C++?

(For n=4)

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1

Printing the above half triangle is quite easy but I couldn't figure out how to print the lower triangle.

This is how I printed the above half triangle.

int num = 1;
for (int i = 1; i <= n; i++)
{
    for (int j = 1; j <= i; j++)
    {   
        cout << num;
        if (i != j)
        {  
             cout << "*";
        } 
          num++;
    }
    cout << endl;
}

Aucun commentaire:

Enregistrer un commentaire