Print Right Triangle Star Pattern
PRINTING PATTERN:
*
**
***
****
PREREQUISITE:
Basic knowledge of C language and use of loops.
ALGORITHM:
- Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
- Run a loop ‘r’ number of times to iterate through each of the rows. From i=0 to i<r. The loop should be structured as for( i=0 ; i<r : i++).
- Run a nested loop inside the main loop to print the stars in each row of the triangle. From j=0 to j<i. The loop should be structured as for( j=0; j<=i ; j++).
- In the nested loop print star.
- In the main loop print a new line.
Code in C:
#include<stdio.h> int main() { int i,j,r; //declaring integer variables i,j for loops and r for number of rows printf("Enter the number of rows :\n"); //Asking user for input scanf("%d",&r); //taking number of rows and saving it in variable r for(i=0;i<r;i++) // loop for number of rows { for(j=0;j<=i;j++) // loop for stars per each row { printf("*"); //printing stars } printf("\n"); // printing newline after each row } }
TAKING INPUT:
DISPLAYING OUTPUT:
Login/Signup to comment
//Code in JavaScript
let row=4;
let col=4;
let i,j,k;
for(i=row;i>0;i–)
{
let str=””;
for(j=row;j>i-1;j–)
{
str=str+”*”;
}
console.log(str);
}
Kindly refer to our discord community for all the technical doubts.
#include
using namespace std;
int main()
{
for(int i=1;i0;j–)
{
cout<<"* ";
}
cout<<"\n";
}
}
int main()
{
for(int i=1;i0;j–)
{
cout<<"* ";
}
cout<<"\n";
}
}
for(int j=i;j>0;j–)
{
cout<<"* ";
}
Code in JAVA:
import java.util.*;
public class Star5 {
public static void main(String arg[])
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i=0; i<=n; i++)
{
for(int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
n=int(input())
for i in range(n+1):
print(‘*’*i,end=” “)
print(“\n”)
n=int(input())
for i in range(0,n):
for j in range(0,n):
if(j<=i):
print('*',end=' ')
else:
print(' ',end=' ')
print()
Code in python
X=int(input(“enter any digit”))
For I in range(0, x) :
For j in range(0, x) :
If(I<j) :
Print(" ", end=" ")
Else:
Print("*", end=" ")
Print()
Code in python:
n=int(input(“enter a number:”)
for i in range(0,n):
print(“*”*(i+1))