Print pyramid triangle in C++

Answer : To print pyramid triangle in C++, we have use two for loop…
  • To print pyramid triangle in C++, we have use two for loop, the outer for loop and the inner for loop.
  • The outer for loop is responsible for rows and the inner for loop is responsible for column.
  • C++ programs to print different types of patterns using stars(*), numbers, and characters or alphabets.
  • This C++ program prints the full pyramid using stars(*).

Sample Code in C++

[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%20%20int%20main()%0A%7B%0A%20%20%20%20int%20j%2C%20rows%3B%0A%20%20%20%20%20%20%20cout%20%3C%3C%22Enter%20number%20of%20rows%3A%20%22%3B%0A%20%20%20%20cin%20%3E%3E%20rows%3B%0A%20%20%20%20%20%20%20for(int%20i%20%3D%201%2C%20k%20%3D%200%3B%20i%20%3C%3D%20rows%3B%20%2B%2Bi%2C%20k%20%3D%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for(j%20%3D%201%3B%20j%20%3C%3D%20rows-i%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%22%20%20%22%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20while(k%20!%3D%202*i-1)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22*%20%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%2Bk%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20endl%3B%0A%20%20%20%20%7D%20%20%20%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”cpp” manual=”Enter%20number%20of%20rows%20%3A%207%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20*%20%0A%20%20%20%20%20%20%20%20%20%20*%20*%20*%20%0A%20%20%20%20%20%20%20%20*%20*%20*%20*%20*%20%0A%20%20%20%20%20%20*%20*%20*%20*%20*%20*%20*%20%0A%20%20%20%20*%20*%20*%20*%20*%20*%20*%20*%20*%20%0A%20%20*%20*%20*%20*%20*%20*%20*%20*%20*%20*%20*%20%0A*%20*%20*%20*%20*%20*%20*%20*%20*%20*%20*%20*%20*%20″ message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *