Drawing A Pyramid

Lesson 5
Author : Afrixi
Last Updated : January, 2023
C - Programming Language
This course covers the basics of programming in C.

To draw a pyramid using asterisks (*) in C, you can use nested loops to control the number of spaces and asterisks printed on each row. Here’s an example of how you could do this:

#include <stdio.h>

int main() {
    int rows, i, j, space;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Let me explain what this code does:

#include <stdio.h>: This line includes the standard input/output library, which provides functions for input and output in C.

int main(): This is the main function of the program, which is where the program starts running. The int before main() indicates that the function returns an integer value.

int i, j, rows;: These are integer variables to store the loop counters and the number of rows to print.

printf("Enter the number of rows: "); This line prints a message to the console to prompt the user to enter the number of rows to print.

scanf("%d", &rows); This line reads the user input from the console and stores it in the rows variable.

for (i = 1; i <= rows; i++) { This is a loop that iterates from 1 to the number of rows to print. It controls the height of the pyramid.

for (j = 1; j <= rows-i; j++) { printf(" "); } This loop prints spaces to align the pyramid to the center of the console. The number of spaces to print decreases as the row number increases. for (j = 1; j <= 2*i-1; j++) { printf("*"); } This loop prints asterisks to form the pyramid shape. The number of asterisks to print increases as the row number increases.

printf("\n"); This prints a newline character to start a new line for the next row of the pyramid.

} This is the end of the code block for the for loop.

return 0; This line indicates that the main function should return an integer value of 0. This is conventionally used to indicate a successful execution of the program.

To run this program, compile it using a C compiler and then execute the resulting program. The program will prompt the user to enter the number of rows to print, and then it will print a pyramid with that many rows to the console.

This program prompts the user to enter the number of rows for the pyramid, reads the input using scanf, and then uses two nested loops to print the correct number of spaces and asterisks for each row.

The outer loop iterates over the rows, from 1 to the number of rows entered by the user. The inner loop prints the correct number of spaces before each row of asterisks, based on the row number. The second inner loop prints the correct number of asterisks for each row, based on the row number.

When you run this program and enter a number of rows, it should output a pyramid made of asterisks with the specified number of rows.