Given a positive integer n. The problem is to print the pyramid pattern as described in the examples below.
Examples:
Input : n = 4
Output :
1
3*2
4*5*6
10*9*8*7
Input : n = 5
Output :
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Source: Amdocs Interview Experience | Set 1
Approach: For odd number row, values are being displayed in increasing order and for even number row, values are being displayed in decreasing order. The only other trick is to how to iterate the loops.
Algorithm:
printPattern(int n)
Declare j, k
Initialize k = 0
for i = 1 to n
if i%2 != 0
for j = k+1, j < k+i, j++
print j and "*"
print j and new line
k = ++j
else
k = k+i-1
for j = k, j > k-i+1, j--
print j and "*";
print j and new line
C++
#include <bits/stdc++.h>
using namespace std;
void printPattern( int n)
{
int j, k = 0;
for ( int i=1; i<=n; i++)
{
if (i%2 != 0)
{
for (j=k+1; j<k+i; j++)
cout << j << "*" ;
cout << j++ << endl;
k = j;
}
else
{
k = k+i-1;
for (j=k; j>k-i+1; j--)
cout << j << "*" ;
cout << j << endl;
}
}
}
int main()
{
int n = 5;
printPattern(n);
return 0;
}
|
Java
public class Pyramid_Pattern {
static void printPattern( int n)
{
int j, k = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (i % 2 != 0 ) {
for (j = k + 1 ; j < k + i; j++)
System.out.print(j + "*" );
System.out.println(j++);
k = j;
}
else {
k = k + i - 1 ;
for (j = k; j > k - i + 1 ; j--)
System.out.print(j + "*" );
System.out.println(j);
}
}
}
public static void main(String args[])
{
int n = 5 ;
printPattern(n);
}
}
|
Python3
def printPattern(n) :
j, k = 0 , 0
for i in range ( 1 , n + 1 ) :
if i % 2 ! = 0 :
for j in range (k + 1 , k + i) :
print ( str (j) + "*" ,
end = "")
j = k + i
print (j)
j + = 1
k = j
else :
k = k + i - 1
for j in range (k, k - i + 1 , - 1 ) :
print ( str (j) + "*" , end = "")
j = k - i + 1
print (j)
if __name__ = = "__main__" :
n = 5
printPattern(n)
|
C#
using System;
public class Pyramid_Pattern {
static void printPattern( int n)
{
int j, k = 0;
for ( int i = 1; i <= n; i++) {
if (i % 2 != 0) {
for (j = k + 1; j < k + i; j++)
Console.Write(j + "*" );
Console.WriteLine(j++);
k = j;
}
else {
k = k + i - 1;
for (j = k; j > k - i + 1; j--)
Console.Write(j + "*" );
Console.WriteLine(j);
}
}
}
public static void Main()
{
int n = 5;
printPattern(n);
}
}
|
PHP
<?php
function printPattern( $n )
{
$k = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $i % 2 != 0)
{
for ( $j = $k + 1; $j < $k + $i ; $j ++)
echo $j . "*" ;
echo $j ++ . "\n" ;
$k = $j ;
}
else
{
$k = $k + $i - 1;
for ( $j = $k ; $j > $k - $i + 1; $j --)
echo $j . "*" ;
echo $j . "\n" ;
}
}
}
$n = 5;
printPattern( $n );
?>
|
Javascript
<script>
function printPattern(n)
{
var j, k = 0;
for ( var i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
for (j = k + 1; j < k + i; j++)
document.write(j + "* " );
document.write(j++ + "<br>" );
k = j;
}
else
{
k = k + i - 1;
for (j = k; j > k - i + 1; j--)
document.write(j + "*" );
document.write(j + "<br>" );
}
}
}
var n = 5;
printPattern(n);
</script>
|
Output:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Time Complexity: O((n * (n + 1)) / 2)
Space Complexity: O(1) since using constant variables, since no extra space has been taken.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.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.