Introduction
In this section, we will break down the problem of the Double Pyramid using an asterisk pattern and find an algorithm to solve the problem. We will also implement the problem statement in C, C++, Java and Python. Solving such problems helps to increase one's knowledge regarding conditionals and loops. It also improves one thinking ability and helps them think out of the box.
Before we solve the problem of Double Pyramid using an asterisk pattern, let us first understand the problem statement in detail.
Problem Statement:- Program to print Double Pyramid using an asterisk pattern
So what does a double pyramid using an asterisk looks like? Look at the below image for reference.
Let us break down the problem statement and get the algorithm for solving the double Pyramid using an asterisk.
In the above problem, we asked the user for the value of N. Based on the user-input value of N, we design the pattern. E.g.,
If the user enters the value of N is 3
If the user enters the value of N is 7
Now we have understood what we want to print, let us look at how we print it. Now to print any pattern, we should ask three questions.
-
How many rows to print?
The answer to the first step is evident just by looking into the pattern. For an input of N, we print (2*N-1) rows.
-
How many columns are in the ith row?
The answer to this part is a little tricky. As we can see from the pattern for the first N rows, we are decreasing the columns as we increase the value of i. After i reach the value of N, our number of columns at ith row starts increasing with an increase in the value of i. We divide the pattern into two-part first part prints the upper Pyramid (Decreasing pyramid), and in the second part, we will print the lower Pyramid (Increasing pyramid).
-
What to print?
The answer to this step is also obvious by looking into the pattern. We print * asterisk character.
Implementing Double Pyramid using an asterisk pattern in C language
Code:-
// Include header files
#include <stdio.h>
int main()
{
// Declare the variables
int row, col, n;
// Ask User for value of N
printf("\nEnter the value of N:- \n");
// Read input
scanf("%d",&n);
// Print the upper pyramid
// Upper pyramid is a decreasing pyramid with each increase in a row no , the no of cols are decreased
// For upper part n rows will be printed
for(row=1; row<=n; row++){
// for each row n-i characters will be printed
for(col=row; col<=n; col++){
// Print the character without adding new line
printf("* ");
}
// Print new line after printing the each row
printf("\n");
}
// Print the lower pyramid
// Lower pyramid is an increasing pyramid with each increase in row no , the no of cols are increased
// For lower part n-1 rows will be printed
for(row=n-1; row>=1; row--){
// for each row i-n+1 characters will be printed
for(col=row; col<=n; col++){
// Print the character without adding new line
printf("* ");
}
// Print new line after printing the each row
printf("\n");
}
}
Output 01:-
Enter the value of N:-
5
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Output 02:-
Enter the value of N:-
7
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *