***** * * * * ** *
Required knowledge:
Basic C programming, If else, For loopBefore printing hollow inverted right triangle star pattern, you must go through inverted right triangle star pattern and hollow right triangle star pattern.
Logic:
You will find this pattern easier if you are done with hollow right triangle and inverted right triangle star patterns. If you haven't here is a simple logic behind this pattern.First is total number of characters(including stars and spaces) per row decreases as row number increases(i.e. first row contains 5 characters and afterwards second contains 4 and so on...) means each row contains n - row_number + 1 characters(where n is the total number of rows).
Second is stars(*) only gets printed for row=1 or column=1 or column=n(Where n is the total number of rows to be printed). And spaces gets printed when stars don't.
Program:
/** * C program to print hollow right triangle star pattern */ #include <stdio.h> int main() { int i, j, n; //Reads number of rows to be printed from user printf("Enter value of n: "); scanf("%d", &n); //Outer loop for iterating over rows for(i=1; i<=n; i++) { for(j=i; j<=n; j++) { /* * Stars gets printed only for row=1, column=1 or column=n */ if(i==1 || j==i || j==n) { printf("*"); } else { //Print spaces if stars don't get printed printf(" "); } } printf("\n"); } return 0; }
Output
Enter value of n: 5
*****
* *
* *
**
*
*****
* *
* *
**
*
Screenshot:
Happy coding ;)
You may also like
- All star patterns programs index.
- For loop programming exercises and solutions.
- If else programming exercises and solutions.
- C program to check whether a triangle is Equilateral, Scalene or Isosceles.
- C program to check whether a triangle is valid or not.
- C program to find profit or loss.
- C program to print table of any number.
- C program to check whether a number is palindrome or not.
- C program to print Pascal triangle of n rows.
- C program to find factorial of any number.
- C program to print fibonacci series.
- C program to check whether a number is Prime number or not.