Right Triangle Star Pattern Program in Python C CPP and Java

 

Half Pyramid or Right Triangle Star (*) Pattern Program in Python, C, CPP (C++) and Java

Here you can find the program to display Half Pyramid or Right Triangle Star Pattern Python C CPP and Java (C++) and Java.

Problem Definition:

Write a program in Python, C, C++ and Java programming language to display the Half Pyramid or Right Triangle Star (*) Pattern.

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

Solution:

The pattern is a two-dimensional matrix. To display any Pattern, we need two loops. First or the outer loop controls the rows and the inner loop controls the columns. Let variable i used to represent the rows and variable j is used to represent the columns. In the above example, i can take values in the range of 0 to 4, and j can take values in the range of 0 to 4.

Now, for every possible value of i, we need to find the possible values of j, where the * is printed.

Here you can find that when i is 0 then the value of j is 0. If the value of i is 1 then possible values of j are 0 and 1. Similarly, if i is 2 the j can take values 0, 1, 2.

Hence the relationship between i and j is, for every possible value of i, j is less than or equal to i or j is less than i+1.

	    j			
      0  1  2  3  4	Possible Values of j
   0  *		        0
   1  *  *		0 1
i  2  *  *  *		0 1 2 
   3  *  *  *  *	0 1 2 3
   4  *  *  *  *  *	0 1 2  3 4
							
	Condition:  j<=i or j < i+1

Half Pyramid or Right Triangle Star (*) Pattern Program in Python

First, read the value of a number of rows using input function and store it into variable n. Then use two for loops, outer for loop controls the rows and inner for loop controls the columns. Use print statements to print the *, whenever the condition is satisfied.

n = int (input("Enter the number of rows: "))
for i in range(n):
    for j in range(i+1):
        print ("*", end= ' ')
    print()

Half Pyramid or Right Triangle Star (*) Pattern Program in C

The required local variable such as n, i, and j is declared. The number of rows in the pattern is read using scanf() function. Use two for loops, outer for loop for rows, and inner for loop for columns. Display the *, whenever the condition is satisfied.

#include <stdio.h>
int main()
{
	int n, i, j;
	printf("Enter the number of rows: ");
	scanf("%d",&n);
	for (i = 0; i< n; i++)
	{
		for (j = 0; j< i+1; j++)
		{
			printf ("*");
		}
		printf("\n");
	}
	return 0;
}

Half Pyramid or Right Triangle Star (*) Pattern Program in C++

#include <stdio.h>
using namespace std;
int main()
{
	int n, i, j;
	cout << "Enter the number of rows: ";
	cin >> n;
	for (i = 0; i< n; i++)
	{
		for (j = 0; j< i+1; j++)
		{
			cout << "*";
		}
		cin >> "\n";
	}
	return 0;
}

Find Frequently asked Pattern Programs in the interview on:

Star Pattern Programs in Python, C, C++ (CPP) and Java

Series Pattern programs in Python, C, C++ (CPP) and Java

Number / Numeric Pattern programs in Python, C, C++ (CPP) and Java

Alphabet / Character Pattern programs in Python, C, C++ (CPP) and Java

This article discusses, how to write a program to display Half Pyramid or Right Triangle Star Pattern in Python, C, CPP (C++), and Java programming language with the video tutorials.

Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.

Leave a Comment

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

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds