In this tutorial you will learn about the C Program to Print Pyramid Star Pattern and its application with practical example.
C Program to Print Pyramid Star Pattern
In this tutorial, we will learn to create a C program that will Print the Pyramid Star Pattern in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the 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.
- Conditional Statements in C programming.
What is a pattern?
The patterns can be any shape or design drawn on the screen i.e. triangle, rectangle, pyramid, any other shape. In the C programming language, the patterns are generated using loops and iterative statements.
What is Printing Pyramid Star Pattern?
The Pyramid star pattern means creating an equilateral triangle like a pattern on the screen for the user. The Pyramid pattern uses “*” to fill the pattern.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the required variables for the program. 2. Taking the input number of rows for plus pattern from the user. 3. Generating the pattern using the nested for loop. 4. Printing the <strong>Pyramid </strong>shaped pattern. 5. End the program. |
Program description:-
In this tutorial, we will create a program, that will print a Pyramid Star Pattern using nested for loops.
First, we will be taking the numbers of rows in form of integer input from the user. Then we will use nested for loops to create a pattern of Pyramid. Then we will print the Pyramid pattern by for loop using the print function. The Pyramid will start from top to bottom.
Below is an example of Print Pyramid Star Pattern.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> //Including the header files int main() //Body of the main function { //Declaring the required variables for the program. int i, j, rw, k = 0; //i = it will hold the integer value for parent loop. //j = it will hold the integer value for child loop. //rw = it will hold the integer value for the input rows. //Taking the input number of rows. printf (" Enter the number of rows: \n"); scanf ("%d", &rw); //rows from the user //Generating the pattern for ( i =1; i <= rw; i++) { for ( j = 1; j <= rw - i; j++) { printf (" "); } // use for loop where k is less than equal to (2 * i -1) for ( k = 1; k <= ( 2 * i - 1); k++) { printf ("* "); // print the Star } printf ("\n"); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- rw = it will hold the integer value.
- j = it will hold the integer value.
- i = it will hold the integer value.
Input the number of rows for the pyramid.
Code to make the pattern on the screen.
Printing output pyramid using the loops and the print function.