In this tutorial you will learn about the C Program to Create Pyramid and its application with practical example.
C Program to Create Pyramid
In this tutorial, we will learn to create a C program that will create a pyramid using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
Create a pyramid:-
The C language is a very powerful programming language. In C programming we can perform many operations with the help of codings. The c language is very easy to create any pattern. In this program, we will learn to create pyramid-like patterns with the help of some code.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
STEP 1: START STEP 2: first declare the variables STEP 3: then start the parent for loop STEP 4: now make the child for loop STEP 5: in this child loop print the blank spaces STEP 6: now use another child loop to start drawing the pyramid STEP 7: Now this for loop will also execute under the parent for loop STEP 8: Print "\n" for changing the line before the increment of the main loop STEP 9: increment of the main for loop STEP 10: return the zero value for main function |
Program:-
To Create a pyramid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { //declaring the variables int i,j,k; //first for loop for lines creation parent for loop for(int i=1;i<=5;i++) { //child for loop for printing the blank spaces for(k=5;k>=i;k--) { printf(" "); } //for loop to print the * pyramid for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value to control parent for loop.
- j = it will hold the integer value to control child for loop.
- k = it will hold the integer value to control child for loop.
Initializing the first parent for loop.
For Loop to print the blank spaces.
For Loop to print the “*”.
Main Program Code.