Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Implementing  Double Pyramid using an asterisk pattern in C language
3.
Implementing  Double Pyramid using an asterisk pattern in C++ language
4.
Implementing  Double Pyramid using an asterisk pattern in Java language
5.
Implementing  Double Pyramid using an asterisk pattern in Python language
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Double Pyramid using asterisk pattern

Author Gaurav Joshi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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  CC++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
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementing  Double Pyramid using an asterisk pattern in C++ language

Code:-

#include<iostream>
#include<cmath>
using namespace std;
int main()
{    
    // Ask User for value of N 
    cout<<"\nEnter the value of N:- \n";
	int n;

	// Read input
	cin>>n;

	// Print the upper pyramid
    // Uper pyramid is a decreasing pyramid with each increase in row no , the no of cols are decreased
    // For upper part n rows will be printed
    for(int row=1; row<=n; row++){  
 
        // for each row n-i characters will be printed
          for(int col=row; col<=n; col++){
                       
               // Print the character without adding new line
                cout<<"* ";
           }
                   
          // Print new line after printing the each row
          cout<<"\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(int row=n-1; row>=1; row--){
        
        // for each row i-n+1 characters will be printed
         for(int col=row; col<=n; col++){
                        
                // Print the character without adding new line
                cout<<"* ";
            }
            
         // Print new line after printing the each row
         cout<<"\n";
    }
}

Output 01:-

Enter the value of N:- 
5
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 

Output 02:-

Enter the value of N:- 
7
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 

Implementing  Double Pyramid using an asterisk pattern in Java language

Code:-

import java.util.Scanner;
public class Double_Pyramid{
    public static void main (String args[]){
        
            // Create Scanner Object for taking Input
            Scanner scan = new Scanner(System.in);
            
            // Ask User for value of N 
            System.out.print("Enter the value of n :- \n");
            
            // Read input with the scanner object
            int n = scan.nextInt();
            
            // Print the upper pyramid
            // Uper pyramid is a decreasing pyramid with each increase in row no , the no of cols   are decreased
            // For upper part n rows will be printed
            for(int row=1; row<=n; row++){   


                // for each row n-i characters will be printed
                  for(int col=row; col<=n; col++){
                       
                       // Print the character without adding new line
                        System.out.print("*"+" ");
                   }
                   
                  // Print new line after printing the each row
                  System.out.println();
            }
            
            // 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(int row=n-1; row>=1; row--){
                // for each row i-n+1 characters will be printed
                 for(int col=row; col<=n; col++){
                        
                        // Print the character without adding new line
                        System.out.print("*"+" ");
                    }
            
                   // Print new line after printing the each row
                  System.out.println();
            }
        
        // Close the scanner object
        scan.close();
    }
}

Output 01:-

Enter the value of n:- 
5
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 

Output 02:-

Enter the value of n:- 
7
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 

Implementing  Double Pyramid using an asterisk pattern in Python language

Code:-

# Ask User for value of N and Read input
n = int(input("Enter the value of 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 in range(1,n+1,1):
 
    # Each row will print n-i characters
    for col in range(row,n+1,1):
 
        # Print the character without adding new line
        print("*",end=" ")
 
    # Print new line after printing each row
    print()

# Print the lower Pyramid 
# Lower Pyramid is an increasing pyramid with each increase in a row no, the no of cols are increased
#  Lower part will print n-1 rows
for row in range(n-1,0,-1):
    
    # Each row  will print i-n+1 characters
    for col in range(row,n+1,1):
    
        # Print the character without adding new line
        print("*",end=" ")
    
    # Print new line after printing each row
    print()  

Output 01:-

Enter the value of N:- 5
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 

Output 02:-

Enter the value of N:- 7
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 

FAQs

  1. What questions should one ask to solve a pattern?
    By answering the below three-question, one could solve many patterns. 
                  How many rows to print?
                  How many columns are in the ith row?
                  What to print?
    The answer to the above question gives you all the necessary information to print most of the patterns.
     
  2. What is a Double Pyramid Pattern?
    A Double pyramid pattern combines two pyramid patterns, i.e. an increasing pyramid and a Decreasing pyramid. We first print the decreasing Pyramid than the increasing Pyramid to form our Double Pyramid.
     
  3. Why is solving more pattern questions beneficial?
    Solving more and more pattern questions helps the programmer to enhance his knowledge and thinking capacity along with a better understanding of loops. To get a good hold of conditional and loop pattern solving is a must.

Key Takeaways

In the blog, we have extensively discussed the problem statement of Printing a Double pyramid using an asterisk pattern and solved the problem in  CC++Java and Python.

I hope this blog has helped you enhance your knowledge and understanding of data structures and algorithms. To solve more such problems visit our Mock Test Series in Code studio. Also, check out the coding ninja guided path for the top 150 interview puzzles. It contains all the essential puzzles usually asked in the interview. Try our Problem Section in Code Studio to be more confident in data structures and algorithms. All the best for your future interviews and Happy Coding till then.
 

Next article
Matrix in Wave Form
Live masterclass