Write a Program to Print Triangle Using * Character

Posted in

Write a Program to Print Triangle Using * Character
vinaykhatri

Vinay Khatri
Last updated on November 23, 2024

    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 from 0 to h .
    • Inside the loop i create a nested loop j that will print the spaces for the triangle's each row.
    • The loop j will start from 0 up to the value of i
    • In the same level of  nested loop j create a loop k that will print the character * for the triangle.
    • The loop of k will start from 0 until its value is greater than i , 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:

    Leave a Comment on this Post

    0 Comments