Program to Print Pyramid Pattern using numbers
Given a number N denoting the number of rows. The task is to print the zigzag pattern with N rows as shown in the below examples.
Examples:
Input : 2
Output : 1
2 3
Input : 5
Output : 1
2 6
3 7 10
4 8 11 13
5 9 12 14 15Approach:
1. Use a for loop for printing the number of rows.
2. Use two for loops for printing space and pattern.
3. Calculate starting value for each row with variable t.
Below is the implementation of above approach:
C++
// C++ Program to print pyramid// pattern using numbers#include <iostream>using namespace std;// Function to print pyramid patternclass gfg{ public : void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) printf("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { printf("%d\t\t", t); t = t + n - k; } printf("\n"); } }};// Driver codeint main(){ gfg g; int n = 6; g.printPattern(n); return 0;}//this code is contributed by Soumik |
C
// C Program to print pyramid// pattern using numbers#include <stdio.h>// Function to print pyramid patternvoid printPattern(int n){ int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) printf("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { printf("%d\t\t", t); t = t + n - k; } printf("\n"); }}// Driver codeint main(){ int n = 6; printPattern(n); return 0;} |
Java
// Java Program to print pyramid// pattern using numbersclass GFG{// Function to print pyramid patternstatic void printPattern(int n){ int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) System.out.print("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { System.out.print(t + "\t\t"); t = t + n - k; } System.out.println(); }}// Driver codepublic static void main(String []args){ int n = 6; printPattern(n);}}// This code is contributed by iAyushRaj |
Python3
# Python3 Program to print pyramid# pattern using numbers# Function to print pyramid patterndef printPattern(n): # outer loop to print rows for i in range(1, n + 1): # inner loop to print spaces for j in range(i, n): print("\t", end = "") # calculate initial value t = i # inner loop to print pattern for k in range (1, i + 1): print(t, "\t", "\t", end = "") t = t + n - k print() # Driver coden = 6printPattern(n) # This code is contributed# by iAyushRaj |
C#
// C# Program to print pyramid// pattern using numbersusing System;class GFG{// Function to print pyramid patternstatic void printPattern(int n){ int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) Console.Write("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { Console.Write(t + "\t\t"); t = t + n - k; } Console.WriteLine(); }}// Driver codepublic static void Main(){ int n = 6; printPattern(n);}}// This code is contributed by iAyushRaj |
PHP
<?php// PHP Program to print pyramid// pattern using numbers// Function to print pyramid patternfunction printPattern($n){ // outer loop to print rows for ($i = 1; $i <= $n; $i++) { // inner loop to print spaces for ($j = $i; $j < $n; $j++) echo "\t"; // calculate initial value $t = $i; // inner loop to print pattern for ($k = 1; $k <= $i; $k++) { echo "$t \t\t"; $t = $t + $n - $k; } echo "\n"; }} // Driver code$n = 6;printPattern($n); // This code is contributed// by iAyushRaj?> |
Javascript
<script> // JavaScript Program to print pyramid // pattern using numbers // Function to print pyramid pattern function printPattern(n) { var t; // outer loop to print rows for (var i = 1; i <= n; i++) { // inner loop to print spaces for (var j = i; j < n; j++) document.write(" "); // calculate initial value t = i; // inner loop to print pattern for (var k = 1; k <= i; k++) { document.write(t + " "); t = t + n - k; } document.write("<br>"); } } // Driver code var n = 6; printPattern(n); </script> |
Output:
1
2 7
3 8 12
4 9 13 16
5 10 14 17 19
6 11 15 18 20 21
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.


