C Program to Print Inverted Right Triangle Star Pattern

Here is a C program to print a inverted right angles triangle pattern of star character using loops. For a inverted right triangle star pattern of 7 rows. Program's output should be:

C program inverted right triangle star pattern

Printing inverted right triangle star patterns is an interesting programming exercise that helps beginners understand loops, conditional statements, and basic arithmetic operations in C.

Required Knowledge

Algorithm to print inverted right triangle star pattern using loop
If you look closely, this pattern is the vertically inverted pattern of right triangle star pattern. As the row number increases from top to bottom, number of stars in a row decreases.
NOTE: The index of rows and columns starts from 0.
  • Take the number of rows(N) of inverted right triangle as input from user using scanf function.
  • Number of stars in Kth row is equal to (N - K + 1). In the pattern given above, N = 7. Hence, 1st row contains 7 star, 2nd row contains 6 stars, 3rd row contains 5 stars.
  • We will use two for loops to print inverted right triangle star pattern.
    • For a inverted right triangle star pattern of N rows, outer for loop will iterate N time(from i = 0 to N-1). Each iteration of outer loop will print one row of the pattern.
    • For jth row of inverted right triangle pattern, inner loop will iterate N-j times(from j = 0 to N-j). Each iteration of inner loop will print one star character.

Here is the matrix representation of the inverted right triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.

C program inverted right triangle star pattern

C program to print inverted 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++) {
        /* Prints one row of triangle */
        for(j = 0; j < rows - i; j++) {
            printf("* ");
        }
        /* move to next row */
        printf("\n");
    }
    return 0;
}
Output
Enter the number of rows
7
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Tips for Writing the Inverted Right Triangle Star Pattern Printing Program in C

As you work on writing the Inverted Right Triangle Star Pattern Printing Program in C, keep the following tips in mind to help you successfully complete the task:
  • Understanding the Pattern : Before you start writing any code, take a moment to understand the pattern. Visualize how the stars are arranged in each row and column to create the inverted right triangle shape.

  • Using Nested Loops : To print the pattern, you'll need to use nested loops. The outer loop will control the number of rows, while the inner loop will control the number of stars in each row. Make sure your loops are structured correctly to achieve the desired pattern.

  • Setting the Size : Use variables to define the size of the inverted right triangle. This will allow you to easily adjust the size of the pattern without having to modify the entire code.

  • Testing Incrementally : As you write your code, test it incrementally. Start with a small inverted right triangle and gradually increase its size to ensure that the pattern is being printed correctly at each step.

  • Debugging and Refining : If you encounter any errors or unexpected behavior, use debugging techniques to identify and resolve the issues. Refine your code iteratively until you achieve the desired inverted right triangle pattern.

  • Experimenting with Variations : Once you have successfully implemented the basic inverted right triangle pattern, try experimenting with variations. You can change the size, orientation, or alignment of the triangle to create different visual effects.

  • Printing Stars and Spaces : Use conditional statements within your nested loops to determine when to print stars and when to print spaces. Pay close attention to the pattern's structure to ensure the correct placement of stars and spaces.

Conclusion

In conclusion, the Inverted Right Triangle Star Pattern Printing Program in C is a valuable exercise for beginners to reinforce looping concepts, enhance problem-solving skills, improve debugging skills, and encourage creativity in programming. Practicing this program is important for beginners to understand fundamental programming concepts and build confidence in their programming abilities. By following the tips provided, beginners can effectively write this program and lay a solid foundation for their programming journey.


Related Topics
C program mirrored right triangle star pattern
C program reversed right triangle star pattern
C program reversed pyramid star pattern
C program hollow diamond star pattern
C program hut star pattern
C program heart shape star pattern
C program natural number triangle pattern
C program same row element triangle pattern
C program binary triangle pattern
List of all C pattern programs