C Exercises: Display the pattern like a pyramid with an asterisk
C For Loop: Exercise-14 with Solution
Write a C program to make such a pattern as a pyramid with an asterisk.
The pattern is as follows :
* * * * * * * * * *
This C program creates a pyramid pattern using asterisks (*). The pattern begins with one asterisk at the top and increases by one asterisk per row, centered to form a pyramid shape. This task demonstrates the use of nested "for" loops and spacing control to produce aligned patterns in C programming.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
void main() {
int i, j, spc, rows, k; // Declare variables 'i' and 'j' for loop counters, 'spc' for spaces, 'rows' for user input, 'k' for loop counter.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
spc = rows + 4 - 1; // Calculate the initial number of spaces.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows.
for (k = spc; k >= 1; k--) { // Loop to print spaces before the asterisks.
printf(" ");
}
for (j = 1; j <= i; j++) { // Loop to print asterisks based on the current row.
printf("* "); // Print an asterisk followed by a space.
}
printf("\n"); // Move to the next line for the next row.
spc--; // Decrement the number of spaces for the next row.
}
}
Output:
Input number of rows : 4 * * * * * * * * * *
Explanation:
for (i = 1; i <= rows; i++) { for (k = spc; k >= 1; k--) { printf(" "); } for (j = 1; j <= i; j++) printf("* "); printf("\n"); spc--; }
In the above first loop, the variable i is initialized to 1, and the loop will continue as long as i is less than or equal to the value of variable 'rows'. In each iteration of the outer loop, the loop variable k is initialized to the value of variable 'spc', and the loop will continue as long as k is greater than or equal to 1.
In each iteration of the inner loop with variable k, the printf function will print space characters to the console. The number of space characters printed will be equal to the value of k.
After the inner loop with variable k completes, another inner loop with variable j is started. In each iteration of this inner loop, the printf function will print an asterisk symbol (*) to the console, followed by a space character. The asterisk symbol will be printed j times, as the value of j is increasing by 1 in each iteration of the inner loop.
After the second inner loop with variable j completes, the outer loop with variable i will print a newline character (\n) to create a new line.
Finally, the value of 'spc' will be decremented by 1, and the process will repeat until the condition i<=rows is no longer true.
Flowchart:
C Programming Code Editor:
Previous: Write a program in C to make such a pattern like a pyramid with numbers increased by 1.
Next: Write a C program to calculate the factorial of a given number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-14.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics