In this article, we will learn how to print inverted right-angle triangles in C, C++, and Python. In the program, we will ask the user to enter the height of the triangle and print the triangle with entered height.
Algorithm
-
Ask the user to enter the height (
h
) of the triangle. -
Create a loop
i
from0
toh
. -
Inside the loop
i
create a nested loopj
that will print the spaces for the triangle's each row. -
The loop
j
will start from0
up to the value ofi
-
In the same level of nested loop
j
create a loopk
that will print the character * for the triangle. -
The loop of
k
will start from0
until its value is greater thani
, with this, the number of stars will be maximum at the initial stage and become less as it goes down.
Print Triangle Pattern in C
#include <stdio.h>
int main()
{
int i, j, k, h;
//input the height of triangle
printf("Enter the Height of Triangle: ");
scanf("%d", &h);
//create a loop
//from 0 to h
for(i =0; i<h; i++)
{
//loop to print spaces
for(j=0;j<i;j++) { printf(" "); } //loop to print stars for(k=h; k>i; --k)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
Enter the Height of Triangle: 12
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
Print Triangle Pattern in C++
#include <iostream>
using namespace std;
int main()
{
int i, j, k, h;
//input the height of triangle
printf("Enter the Height of Triangle: ");
cin>>h;
//create a loop
//from 0 to h
for(i =0; i<h; i++)
{
//loop to print spaces
for(j=0;j<i;j++)
{
cout<<" "; } //loop to print stars for(k=h; k>i; --k)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}
Output
Enter the Height of Triangle: 15
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
Print Triangle Pattern in Python
#input the height of triangle
h = int(input("Enter the Height of Triangle: "))
# create a loop
# from 0 to h
for i in range(h):
# loop to print spaces
for j in range(i):
print(" ", end="")
# loop to print stars
for k in range(h, i, -1):
print("*", end="")
print()
Output
Enter the Height of Triangle: 15
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
Complexity Analysis
- Time Complexity: O(N^2), because we have used a nested loop.
- Space Complexity: O(1), because we have not used any extra space.
Wrapping Up!
In this article, we learned how to print triangles in a console using C, C++, and Python programming languages. In this tutorial, we print the inverted right angle triangle, in which we first ask the user to enter the height of the triangle and accordingly print the triangle using the stars character.
People are also reading:
- Construct a Binary Tree from Inorder and Preorder traversals
- Print a given matrix in spiral form
- Program to Rotate an Array
- Print all subarrays with 0 sum
- Longest subarray with sum as K
- Merge Two Sorted Arrays in-place
- Sort an array in one swap whose two elements are swapped
- Longest Palindromic Subsequence using Dynamic Programming
- The Stock Span Problem
- Rearrange an array such that arr[i] = i
Leave a Comment on this Post