w3resource

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 :

   * 
  * * 
 * * * 
* * * *

Visual Presentation:

Display the pattern like a pyramid with an asterisk

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.

int 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.
   }
    return 0;  // Indicate that the program has executed successfully.
}

Sample 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:

Flowchart: Display the pattern like pyramid with asterisk

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.



Follow us on Facebook and Twitter for latest update.