Program to print pyramid pattern - GeeksforGeeks

20 Jan, 2022
Write to program to print the pyramid pattern formed of stars 
Example : 
Input:  n = 6
Output:
       *
       * *
       * * *
       * * * *
       * * * * *
       * * * * * * 
       * * * * *
       * * * *
       * * *
       * * 
       *
We strongly recommend you to minimize your browser and try this yourself first.The idea is to use two for loops for every part of the pyramid. The two parts may be classified as upper part and lower part 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to print Pyramid pattern#include<iostream>using namespace std; void pattern(int n){       // For printing the upper part of the pyramid    for (int i = 1; i < n; i++){        for (int j = 1; j < i+1; j++){            cout <<" * ";        }        cout << endl ;    }     // For printing the lower part of pyramid    for (int i = n; i > 0; i--){        for (int j = i; j > 0; j--){            cout <<  " * ";        }        cout << endl ;    }} // Driver programint main(){    pattern(6);    return 0;}
// Java program to print Pyramid patternimport java.io.*; class GFG{    public static void pattern(int n)    {        // For printing the upper        // part of the pyramid        for (int i = 1; i < n; i++)        {            for (int j = 1; j < i+1; j++)            {                System.out.print(" * ");            }            System.out.println();        }             // For printing the lower        // part of pyramid        for (int i = n; i > 0; i--)        {            for (int j = i; j > 0; j--)            {                System.out.print(" * ");            }            System.out.println();        }    }         // Driver program    public static void main(String args[])    {        pattern(6);    }}     // This code is contributed by NIkita Tiwari.
# Python program to print Pyramid pattern def pattern(n):        # For printing the upper part of pyramid    for i in range (1, n+1):        for j in range (1, i+1):            print (" *",end=" ")        print()         # for printing the middle and lower part of pyramid    for i in range (n, 1, -1):        for j in range (i, 1, -1):            print (" *",end=" ")        print() # Driver programpattern(6)
// C# program to print Pyramid patternusing System; class GFG {    public static void pattern(int n)    {        // For printing the upper        // part of the pyramid        for (int i = 1; i < n; i++) {            for (int j = 1; j < i + 1; j++) {                Console.Write(" * ");            }            Console.WriteLine();        }         // For printing the lower        // part of pyramid        for (int i = n; i > 0; i--) {            for (int j = i; j > 0; j--) {                Console.Write(" * ");            }            Console.WriteLine();        }    }     // Driver program    public static void Main()    {        pattern(6);    }} // This code is contributed by vt_m.
<?php// PHP implementation to print// Pyramid pattern function pattern($n){    // For printing the upper part    // of the pyramid    for ($i = 1; $i < $n; $i++)    {        for ($j = 1; $j < $i+1; $j++)        {            echo " * ";        }        echo "\n" ;    }     // For printing the lower part    // of pyramid    for ($i = $n; $i > 0; $i--)    {        for ($j = $i; $j > 0; $j--)        {            echo " * ";        }        echo "\n" ;    }} // Driver code$n=6;pattern($n); // This code is contributed by mits?>
<script> // JavaScript program to print Pyramid patternfunction pattern(n){         // For printing the upper    // part of the pyramid    for(var i = 1; i < n; i++)    {        for(var j = 1; j < i + 1; j++)        {            document.write(" * ");        }        document.write("<br>");    }         // For printing the lower part of pyramid    for(var i = n; i > 0; i--)    {        for(var j = i; j > 0; j--)        {            document.write(" * ");        }        document.write("<br>");    }} // Driver codepattern(6); // This code is contributed by rdtank </script>
Output : 
 *
 *  *
 *  *  *
 *  *  *  *
 *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *
 *  *  *  *
 *  *  *
 *  *
 *
This article is contributed by Rahul Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Mithun Kumar
rdtank
amartyaghoshgfg
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
Operator Overloading in C++
Overriding in Java
Friend class and function in C++
Polymorphism in C++
Types of Operating Systems
Python program to check if a string is palindrome or not
Inheritance in Java
Constructors in Java
Different ways of Reading a text file in Java