C Program to Print Star Pattern

Printing star patterns in C is a fundamental exercise that helps programmers understand loop constructs and their applications. In this article, we’ll explore three different methods of program to print star pattern in C, complete with code examples, explanations, and output. These methods include the right-angled triangle pattern, the inverted right-angled triangle pattern, and the pyramid pattern.

Write a C Program to Print Star Pattern

Introduction to Star Patterns

Teachers use star patterns as a classic exercise to teach and reinforce basic programming concepts like loops and nested loops. By practicing these patterns, you can improve your coding skills and gain a deeper understanding of how to control program flow.

Method 1) Right-Angled Triangle Star Pattern

Code Example

Here is a simple C program to print a right-angled triangle star pattern:

#include <stdio.h>

int main() {
    int i, j, rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    
    for(i = 1; i <= rows; i++) {
        for(j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Explanation of the Code

  • Lines 1-2: The program includes the standard input-output library.
  • Line 4: The main function begins.
  • Lines 5-7: The variables i, j, and rows are declared. The program prompts the user to enter the number of rows.
  • Lines 8-12: The outer loop iterates from 1 to rows. For each iteration of the outer loop, the inner loop prints stars up to the current row number (i). After completing each row, the program prints a newline character to move to the next line.

Output

For rows = 5, the output will be:

* 
* * 
* * * 
* * * * 
* * * * * 

Method 2) Inverted Right-Angled Triangle Star Pattern

Code Example

This program prints an inverted right-angled triangle star pattern:

#include <stdio.h>

int main() {
    int i, j, rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    
    for(i = rows; i >= 1; i--) {
        for(j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Explanation of the code

  • Lines 1-2: Similar to the previous method, the program includes the necessary library.
  • Line 4: The main function begins.
  • Lines 5-7: We declare variables and take user input
  • Lines 8-12: The outer loop runs from rows down to 1, decrementing i each time. The inner loop prints stars from 1 to i, creating an inverted triangle pattern. We declare variables and prompt the user for input.

Output

For rows = 5, the output will be:

* * * * * 
* * * * 
* * * 
* * 
* 

Method 3) Pyramid Star Pattern

Code Example

To print a pyramid star pattern, use the following program:

#include <stdio.h>

int main() {
    int i, j, k, rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    
    for(i = 1; i <= rows; i++) {
        for(j = i; j < rows; j++) {
            printf(" ");
        }
        for(k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Explanation of the Code

  • Lines 1-2: The standard input-output library is included.
  • Line 4: The main function starts.
  • Lines 5-7: Variables are declared, and user input is read.
  • Lines 8-15: The outer loop iterates from 1 to rows. The first inner loop prints spaces to align the stars in a pyramid shape. The second inner loop prints stars based on the current row number (i). A newline character is printed after each row to form the pyramid shape.

Output

For rows = 5, the output will be:

    *
   ***
  *****
 *******
*********

Understanding and implementing star patterns in C is a great way to strengthen your programming fundamentals. Each pattern—right-angled triangle, inverted right-angled triangle, and pyramid—demonstrates different ways to manipulate loops and output. Practicing these patterns will not only improve your coding skills but also provide a solid foundation for more complex programming tasks.

Categories C

Leave a Comment