given an integer n the number of rows in a * triangle, write a program to get the triangle with n rows. in Python or c
Answers
Answer:
# Function to demonstrate printing pattern triangle
def triangle(n):
# number of spaces
k = 2*n - 2
# outer loop to handle number of rows
for i in range(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range(0, k):
print(end=" ")
# decrementing k after each loop
k = k - 1
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
triangle(n)
Explanation:
Self Explainatory.
Answer:
#include<stdio.h>
int main()
{
int i, j ,n;
printf("enter no.of rows n");
scanf("%d",&n);
//making a loop for making a triangle.
for (i=1;i<=n;i++)
{
for (j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
Explanation:
In the code we had made two loops in a nested form where the i loop increases the row after each iterations and the j loop prints "*" i times to make the triangle.
The i,j,n variables are initiated as integer.
The scanf function takes input for the user and store it in the memory corresponding to integer n.
#spj3