- Write a program in C to print mirrored right angled triangle star (*) pattern using for loop.
- Write a C program to print a mirrored right angles triangle pattern of star (*) character of n rows.
For a mirrored right triangle star pattern of 7 rows. Program’s output should be:
Required Knowledge
- For Loop
- printf and scanf function in C
Algorithm to print mirrired right triangle star pattern using loop
C program to print mirrored right triangle star pattern is similar to the right triangle star pattern program. Once you understand how to print right triangle pattern then printing it’s mirrored pattern will be an easy task.
- Take the number of rows(N) of mirrored right triangle as input from user using scanf function.
- In any row, the sum of spaces and stars are equal to N. Number of stars increases by one and number of spaces before stars decreases by 1 in consecutive rows.
- In any row R, we will first print N-R-1 space characters then R+1 star characters.
Here is the matrix representation of the inverted triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
C program to print mirrored right triangle star pattern
#include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows\n"); scanf("%d", &rows); for(i = 0; i < rows; i++){ /* for j th row, first print rows-r spaces then stars */ for(j = 0; j < rows; j++){ if(j < rows-i-1){ printf(" "); } else { printf("*"); } } /* move to next row */ printf("\n"); } return 0; }
Output
Enter the number of rows 6 * ** *** **** ***** ******
Here is the C program to print inverted right triangle star pattern using one for loop
#include<stdio.h> int main(){ char *str="*******************"; int i,j, rows; printf("Enter the number of rows\n"); scanf("%d", &rows); for(i = 0; i < rows; i++){ printf("%*.*s\n", rows, i+1, str); } return 0; } </stdio.h>
Output
Enter the number of rows 5 * ** *** **** *****