Program to Print the Following Triangle

Posted in

Program to Print the Following Triangle
vinaykhatri

Vinay Khatri
Last updated on December 26, 2024

    Problem Statement 1

    We need to write a program in C, C++, and Python that can print the following triangle pattern, based on the user entered height. Let's say the height of the triangle is 8

    Input:

    height =8

    Output

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    

    Algorithm

    The algorithm of this problem is pretty straightforward.

    • Ask the user to enter the height of the triangle.
    • Create an Outer for loop i from range 1 to h , where h is the height of the triangle.
    • Inside the Outer loop create an inner loop j from range 1 to i , that will print the number of stars.

    C Program to Print the Triangle

    #include <stdio.h>
    int main()
    {
    	int h, i, j;
    	
    	//ask user to input the height of the triangle
    	printf("Enter the height of Triangle: ");
    	scanf("%d", &h);
    	
    	//outer loop i
    	for(i =1; i<=h; i++)
    	{
    		for(j=1; j<=i; j++)
    		{
    			printf("*");
    		}
    		
    		//after printing every row of stars
    		//we need to print new row of stars in a new line
    		printf("\n");
    	}
    	
        return 0;
    }

    Output

    Enter the height of Triangle: 8
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********

    C++ Program to Print the Triangle

    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int h, i, j;
    	
    	//ask user to input the height of the triangle
    	cout<<"Enter the height of Triangle: "; cin>>h;
    	
    	//outer loop i
    	for(i =1; i<=h; i++)
    	{
    		//inner loop
    		for(j=1; j<=i; j++)
    		{
    			cout<<"*";
    		}
    		
    		//after printing every row of stars
    		//we need to print new row of stars in a new line
    		cout<<endl;
    	}
        return 0;
    }

    Output

    Enter the height of Triangle: 9
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********

    Python Program to Print the Triangle

    #input the height of the triangle
    h = int(input("Enter the Height of the triangle: "))
    
    #outer loop
    for i in range(1,h+1):
        #inner loop
        for j in range(1, i+1):
            print("*", end="")
    
        #print a new line after printing a row of stars
        print()

    Output

    Enter the Height of the triangle: 10
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    Complexity Analysis

    • Time Complexity: As we are using a nested loop in the program, which makes the time complexity O(N 2 ) .
    • Space Complexity: There is no extra space occupancy in the program so the space complexity is constant.

    Problem Statement 2

    Now we need to print an inverted triangle of the above triangle pattern.

    Input:

    height =8

    Output

           *
          **
         ***
        ****
       *****
      ******
     *******
    ********
    

    Algorithm

    To make such a pattern we require 3 loops one outer loop i and two inner loops j and k . Where the j loop will be responsible for printing white spaces and k for printing stars.

    • Ask the user to enter the height of the triangle h .
    • Create an outer for loop i from range 1 to h .
    • Inside the outer loop create an inner nested loop j from range 1 to h-i, and it will print the white spaces.
    • Inside the outer loop, we also need to create a nested loop k from range 1 to i , that will print the stars.
    • Both j and k loops will be nested to i and have the same level.

    C Program to Print the Triangle

    #include <stdio.h>
    int main()
    {
    	int h, i, j, k;
    	
    	//ask user to input the height of the triangle
    	printf("Enter the height of Triangle: ");
    	scanf("%d", &h);
    	
    	//outer loop i
    	for(i =1; i<=h; i++)
    	{
    		//inner loop 1
    		//to print white space
    		for(j=1; j<=h-i; j++)
    		{
    			printf(" ");
    		}
    		
    		//inner loop 2 
    		//to print stars
    		for(k=1; k<=i; k++)
    		{
    			printf("*");
    		}
    		//after printing every row of stars
    		//we need to print new row of stars in a new line
    		printf("\n");
    	}
    	
        return 0;
    }

    Output

    Enter the height of Triangle: 18
                     *
                    **
                   ***
                  ****
                 *****
                ******
               *******
              ********
             *********
            **********
           ***********
          ************
         *************
        **************
       ***************
      ****************
     *****************
    ******************

    C++ Program to Print the Triangle

    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int h, i, j,k;
    	
    	//ask user to input the height of the triangle
    	cout<<"Enter the height of Triangle: "; cin>>h;
    	
    	//outer loop i
    	for(i =1; i<=h; i++)
    	{
    		//inner loop 1
    		// to print white spaces
    		for(j=1; j<=h-i; j++)
    		{
    			cout<<" ";
    		}
    		
    		//inner loop 2
    		// to print the stars
    		for(k =1; k<=i; k++)
    		{
    			cout<<"*";
    		}
    		//after printing every row of stars
    		//we need to print new row of stars in a new line
    		cout<<endl;
    	}
        return 0;
    }

    Output

    Enter the height of Triangle: 6
         *
        **
       ***
      ****
     *****
    ******

    Python Program to Print the Triangle

    #input the height of the triangle
    h = int(input("Enter the Height of the triangle: "))
    
    #outer loop
    for i in range(1,h+1):
    
        #inner loop 1 to print white spaces
        for j in range(1, (h-i)+1):
            print(" ", end="")
    
        #inner loop 2, to print stars
        for k in range(1, i+1):
            print("*", end="")
    
    
        #print a new line after printing a row of stars
        print()

    Output

    Enter the Height of the triangle: 5
        *
       **
      ***
     ****
    *****

    Complexity Analysis

    Time Complexity: O(N 2 ) , The j and k loops are in the same level nested to the i loop so the time complexity of the above program is O(N 2 ).

    Space Complexity: O(1)

    Problem Statement 3

    Now we need to print a hollow triangle based on the height input by the user.

    Example Input

    h = 6

    Output

         *
        * *
       *   *
      *     *
     *       *
    ***********

    Algorithm

    • Ask the user to enter the height of the triangle h .
    • Create a new variable space s and set its value equal to h (height of the triangle).
    • Create an outer loop i from range 1 to h.
    • Inside the Outer loop create the inner-loop-1 j from 1 to s-1 that will print the white spaces before every first star of the row.
    • Inside the outer loop in the same level of inner-loop-1, create another inner-loop-2 k that will range from 1 to 2*i , and it will print starts for only values k==1 || k==2*i-1 || i==height and for the rest of the values, it will print white spaces.
    • With every iteration of the outer loop decrease the value of space s by 1.

    C Program to Print the Triangle

    #include <stdio.h>
    int main()
    {
    	int h, s, i, j, k;
    	
    	//ask user to input the height of the triangle
    	printf("Enter the height of Triangle: ");
    	scanf("%d", &h);
    	
    	// set the space value equal to the height 
    	s=h;
    	//outer loop i
    	for(i =1; i<=h; i++)
    	{
    		//inner loop 1
    		//to print white space
    		for(j=1; j<=s-1; j++)
    		{
    			printf(" ");
    		}
    		
    		//inner loop 2 
    		//to print stars and spaces between the stars
    		for(k=1; k<=2*i-1; k++)
    		{
    			if(k==1 || k==2*i-1 || i==h)
    				printf("*");
    			else
    				printf(" ");
    		}
    		
    		//decrement the value of s by 1
    		s--;
    		
    		//after printing every row of stars
    		//we need to print new row of stars in a new line
    		printf("\n");
    	}
    	
        return 0;
    }

    Output

    Enter the height of Triangle: 9
            *
           * *
          *   *
         *     *
        *       *
       *         *
      *           *
     *             *
    *****************

    C++ Program to Print the Triangle

    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int h,s, i, j,k;
    	
    	//ask user to input the height of the triangle
    	cout<<"Enter the height of Triangle: "; cin>>h;
    	
    	// set the space value equal to the height 
    	s=h;
    	
    	//outer loop i
    	for(i =1; i<=h; i++)
    	{
    		//inner loop 1
    		// to print white spaces
    		for(j=1; j<=s-1; j++)
    		{
    			cout<<" ";
    		}
    		
    		//inner loop 2
    		// to print the stars and spaces between stars
    		for(k=1; k<=2*i-1; k++)
    		{
    			if(k==1 || k==2*i-1 || i==h)
    				printf("*");
    			else
    				printf(" ");
    		}
    		
    		//decrement the value of s by 1
    		s--;
    		//after printing every row of stars
    		//we need to print new row of stars in a new line
    		cout<<endl;
    	}
        return 0;
    }

    Output

    Enter the height of Triangle: 7
          *
         * *
        *   *
       *     *
      *       *
     *         *
    *************

    Python Program to Print the Triangle

    #input the height of the triangle
    h = int(input("Enter the Height of the triangle: "))
    
    #set the space variable value equal to the height
    s= h
    
    #outer loop
    for i in range(1,h+1):
        #inner loop 1 to print white spaces
        for j in range(1, s):
            print(" ", end="")
        for k in range(1, i*2):
            if k==1 or k==2*i-1 or i==h:
                print("*", end="")
            else:
                print(" ", end="")
    
        #decrement the value of space by 1
        s-=1
        #print a new line after printing a row of stars
        print()

    Output

    Enter the Height of the triangle: 10
             *
            * *
           *   *
          *     *
         *       *
        *         *
       *           *
      *             *
     *               *
    *******************

    Complexity Analysis

    • Time Complexity: O(N 2 ) ,
    • Space Complexity: O(1)

    Wrapping UP!

    In this Programming tutorial, we learned how to print a given tringle pattern in C, C++, and Python. Printing a triangle pattern is one of the classical programming interview questions , there are many tringle patterns that we can print using nested loops. These triangle patterns are also known as pyramid patterns.

    To know how to print different types of Pyramid patterns click here .

    People are also reading:

    Leave a Comment on this Post

    0 Comments