GoogleTag

Google Search

[Programming] Question 31: Write a Program to create a pyramid pattern using C

 

#include <stdio.h>

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

	printf("Enter 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;
} 
Inside the main() function:

- Integer variables `rows`, `space`, `i`, and `j` are declared to store the number of rows, spaces, and loop iterators.

- The user is prompted to enter the number of rows.

- Two nested loops are used to print the pyramid pattern.

- The first loop iterates through each row.

- The second loop prints spaces before the stars for each row.

- Another loop prints the stars for each row.

- The program prints a newline after each row.

 

Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts