C Program to print pyramid pattern - GeeksforGeeks

25 Oct, 2021
Given a positive integer n. Task is to print the pyramid pattern as described in the examples below.Examples:  
Input : 2
Output :
        1
        22
        1

Input : 3
Output :
        1
        22
        333
        22
        1

Input : 5
Output : 
        1
        22
        333
        4444
        55555
        4444
        333
        22
        1
 
Algorithm:  
//Print All Pattern
printPattern(int N)
    // Print upper pattern
    /* 1
       22
       333
       ...
       N...(N)times   */
    for i->1 to N
        for j->1 to i
            print i
        print next line

    // Print lower triangle
    /* N-1....(N-1)times
       ...
       333
       22
       1 */
    for i->N-1 to 0
        for j->i to 0
            print i
        print next line
 
C++
Java
Python3
JavaScript
// C++ program to print the pyramid pattern#include <bits/stdc++.h>using namespace std; // Print the pattern upto nvoid printPattern(int n){    // Printing upper part    for (int i = 1; i <= n; i++)    {        for (int j = 1; j <= i; j++)            cout << i;               cout << "\n";    }     // printing lower part    for (int i = n - 1; i > 0; i--)    {       for (int j = i; j > 0; j--)            cout << i;               cout << "\n";    }} // Driver Codeint main(){    int n = 8;    printPattern(n);    return 0;}
// Java program to print the pyramid pattern class GFG {     // Print the pattern upto nstatic void printPattern(int n){    // Printing upper part    for (int i = 1; i <= n; i++) {    for (int j = 1; j <= i; j++)        System.out.print(i);    System.out.print("\n");    }     // printing lower part    for (int i = n - 1; i > 0; i--) {    for (int j = i; j > 0; j--)        System.out.print(i);    System.out.print("\n");    }} // Driver codepublic static void main(String arg[]){    int n = 8;    printPattern(n);}} // This code is contributed by Anant Agarwal.
# Python program to print# the pyramid pattern # Print the pattern upto ndef printPattern(n):     # Printing upper part    for i in range(n+1):             for j in range(1,i+1):            print(i,end="")               print("")         # printing lower part    for i in range(n - 1,0,-1):             for j in range(i,0,-1):            print(i,end="")               print("") # driver coden = 8printPattern(n) # This code is contributed# by Anant Agarwal.
C#] 


<?php
// php program to print the
// pyramid pattern

// Print the pattern upto n
function printPattern($n)
{
    // Printing upper part
    for ($i = 1; $i <= $n; $i++) 
    {
        for ($j = 1; $j <= $i; $j++) 
            echo $i;     
        echo "\n";
    }

    // printing lower part
    for ($i = $n - 1; $i > 0; $i--) 
    {
    for ($j = $i; $j > 0; $j--) 
            echo $i;     
        echo "\n";
    }
}

    // Driver Code
    $n = 8;
    printPattern($n);

// This code is contributed by mits    
?>


<script> // javascript program to print the pyramid pattern  // Print the pattern upto nfunction printPattern(n){    // Printing upper part    for (var i = 1; i <= n; i++) {        for (var j = 1; j <= i; j++)            document.write(i);        document.write("<br>");    }     // printing lower part    for (var i = n - 1; i > 0; i--) {        for (var j = i; j > 0; j--)            document.write(i);        document.write("<br>");    }} // Driver codevar n = 8;printPattern(n);  // This code is contributed by 29AjayKumar</script>
Output:  
1
22
333
4444
55555
666666
7777777
88888888
7777777
666666
55555
4444
333
22
1
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Mithun Kumar
29AjayKumar
ankita_saini
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
Polymorphism in C++
Types of Operating Systems
Friend class and function in C++
Different ways of Reading a text file in Java
Constructors in Java
Inheritance in Java
Python program to check if a string is palindrome or not