Given an integer n. Write a program to print the Right angle triangle wall. The length of perpendicular and base is s.
Note: Print exactly single " " after "*". Print a new line after printing the triangle.

Examples:
Input: n = 4
Output:
*
* *
* * *
* * * *
Explanation: Length of perpendicular and base of triangle is 4 .Input: n = 3
Output:
*
* *
* * *
Explanation: Length of perpendicular and base of triangle is 3.
Try It Yourself
Nested Loops ā O(n ^ 2) Time and O(1) Space
The idea is to use two nested loops to print a right-angled triangle pattern. For each row, print stars equal to the row number.
Working of Approach:
- Run the outer loop from 1 to n to traverse each row.
- For each row i, run the inner loop from 1 to i.
- Print "* " in each iteration of the inner loop.
- After printing all stars in a row, use endl to move to the next line.
- Repeat until all n rows are printed.
#include <iostream>
using namespace std;
void printPattern(int n)
{
// Traverse each row
for (int i = 1; i <= n; i++)
{
// Print stars equal to the row number
for (int j = 1; j <= i; j++)
{
cout << "* ";
}
// Move to the next line
cout << endl;
}
}
int main()
{
int n = 4;
printPattern(n);
return 0;
}
#include <stdio.h>
void printPattern(int n)
{
// Traverse each row
for (int i = 1; i <= n; i++)
{
// Print stars equal to the row number
for (int j = 1; j <= i; j++)
{
printf("* ");
}
// Move to the next line
printf("\n");
}
}
int main()
{
int n = 4;
printPattern(n);
return 0;
}
public class GFG {
public static void printPattern(int n)
{
// Traverse each row
for (int i = 1; i <= n; i++) {
// Print stars equal to the row number
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
// Move to the next line
System.out.println();
}
}
public static void main(String[] args)
{
int n = 4;
printPattern(n);
}
}
def printPattern(n):
# Traverse each row
for i in range(1, n + 1):
# Print stars equal to the row number
for j in range(1, i + 1):
print("* ", end="")
# Move to the next line
print()
if __name__ == "__main__":
n = 4
printPattern(n)
using System;
class GFG {
static void printPattern(int n)
{
// Traverse each row
for (int i = 1; i <= n; i++) {
// Print stars equal to the row number
for (int j = 1; j <= i; j++) {
Console.Write("* ");
}
// Move to the next line
Console.WriteLine();
}
}
static void Main()
{
int n = 4;
printPattern(n);
}
}
function printPattern(n)
{
// Traverse each row
for (let i = 1; i <= n; i++) {
let line = "";
// Print stars equal to the row number
for (let j = 1; j <= i; j++) {
line += "* ";
}
// Move to the next line
console.log(line);
}
}
// Driver Code
let n = 4;
printPattern(n);
Output
* * * * * * * * * *