Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmPrint the following pyramid pattern

Print the following pyramid pattern

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++




// C++ implementation to print the following 
// pyramid pattern 
#include <bits/stdc++.h> 
using namespace std; 
  
// function to print the following pyramid pattern 
void printPattern(int n) 
    int j, k = 0; 
      
    // loop to decide the row number 
    for (int i=1; i<=n; i++) 
    
        // if row number is odd 
        if (i%2 != 0) 
        
            // print numbers with the '*' sign in 
            // increasing order 
            for (j=k+1; j<k+i; j++) 
                cout << j << "*"
            cout << j++ << endl; 
              
            // update value of 'k'     
            k = j;     
        
          
        // if row number is even 
        else
        
            // update value of 'k' 
            k = k+i-1; 
              
            // print numbers with the '*' in 
            // decreasing order 
            for (j=k; j>k-i+1; j--) 
                cout << j << "*"
            cout << j << endl;     
        
    
  
// Driver program to test above 
int main() 
    int n = 5; 
    printPattern(n); 
    return 0; 


Java




// Java implementation to print the 
// following pyramid pattern 
public class Pyramid_Pattern { 
  
    // function to print the following pyramid 
    // pattern 
    static void printPattern(int n) 
    
        int j, k = 0
  
        // loop to decide the row number 
        for (int i = 1; i <= n; i++) { 
              
            // if row number is odd 
            if (i % 2 != 0) { 
              
                // print numbers with the '*' 
                // sign in increasing order 
                for (j = k + 1; j < k + i; j++) 
                    System.out.print(j + "*"); 
                System.out.println(j++); 
  
                // update value of 'k' 
                k = j; 
            
  
            // if row number is even 
            else
                  
                // update value of 'k' 
                k = k + i - 1
  
                // print numbers with the '*' in 
                // decreasing order 
                for (j = k; j > k - i + 1; j--) 
                    System.out.print(j + "*"); 
                System.out.println(j); 
            
        
    
  
    // Driver program to test above 
public static void main(String args[]) 
    
        int n = 5
        printPattern(n); 
    
// This code is contributed by Sumit Ghosh 


Python3




# Python3 implementation to print the 
# following pyramid pattern 
  
# function to print the 
# following pyramid pattern 
def printPattern(n) : 
  
    j, k = 0, 0
  
    # loop to decide the row number 
    for i in range(1, n + 1) : 
  
        # if row number is odd 
        if i % 2 != 0
  
            # print numbers with 
            # the '*' sign in 
            # increasing order 
            for j in range(k + 1, k + i) : 
  
                print(str(j) + "*"
                        end = "") 
  
            j = k +
            print(j) 
            j += 1
  
            # update value of 'k' 
            k =
  
        # if row number is even 
        else
  
            # update value of 'k' 
            k = k + i - 1
  
            # print numbers with the 
            # '*' in decreasing order 
            for j in range(k, k - i + 1, -1) : 
                print(str(j) + "*", end = "") 
  
            j = k - i + 1
            print(j) 
  
# Driver Code 
if __name__ == "__main__"
    n = 5
  
    # function calling 
    printPattern(n) 
              
# This code is contributed 
# by ANKITRAI1 


C#




// C# implementation to print the 
// following pyramid pattern 
using System; 
public class Pyramid_Pattern { 
  
    // function to print the following pyramid 
    // pattern 
    static void printPattern(int n) 
    
        int j, k = 0; 
  
        // loop to decide the row number 
        for (int i = 1; i <= n; i++) { 
              
            // if row number is odd 
            if (i % 2 != 0) { 
              
                // print numbers with the '*' 
                // sign in increasing order 
                for (j = k + 1; j < k + i; j++) 
                    Console.Write(j + "*"); 
                Console.WriteLine(j++); 
  
                // update value of 'k' 
                k = j; 
            
  
            // if row number is even 
            else
                  
                // update value of 'k' 
                k = k + i - 1; 
  
                // print numbers with the '*' in 
                // decreasing order 
                for (j = k; j > k - i + 1; j--) 
                    Console.Write(j + "*"); 
                    Console.WriteLine(j); 
            
        
    
  
    // Driver program to test above 
public static void Main() 
    
        int n = 5; 
        printPattern(n); 
    
// This code is contributed by vt_m. 


PHP




<?php 
// PHP implementation to print the 
// following pyramid pattern 
  
// function to print the following 
// pyramid pattern 
function printPattern($n
    $k = 0; 
      
    // loop to decide 
    // the row number 
    for ($i = 1; $i <= $n; $i++) 
    
          
        // if row number is odd 
        if ($i % 2 != 0) 
        
              
            // print numbers with the '*' 
            // sign in increasing order 
            for ($j = $k + 1; $j < $k + $i; $j++) 
                echo $j. "*"
            echo $j++ ."\n"
              
            // update value of 'k' 
            $k = $j
        
          
        // if row number is even 
        else
        
              
            // update value of 'k' 
            $k = $k + $i - 1; 
              
            // print numbers with the 
            // '*' in decreasing order 
            for ($j = $k; $j > $k - $i + 1; $j--) 
                echo $j . "*"
            echo $j ."\n"
        
    
  
    // Driver Code 
    $n = 5; 
    printPattern($n); 
  
// This code is contributed by Sam007 
?> 


Javascript




<script>
  
// JavaScript implementation to print the
// following pyramid pattern
  
// Function to print the following 
// pyramid pattern 
function printPattern(n)
{
    var j, k = 0;
      
    // Loop to decide the row number
    for(var i = 1; i <= n; i++)
    {
          
        // If row number is odd
        if (i % 2 != 0)
        {
              
            // Print numbers with the '*'
            // sign in increasing order
            for(j = k + 1; j < k + i; j++)
                document.write(j + "* ");
                  
            document.write(j++ + "<br>");
  
            // Update value of 'k'
            k = j;
        }
  
        // If row number is even
        else 
        {
              
            // Update value of 'k'
            k = k + i - 1;
  
            // Print numbers with the '*' in
            // decreasing order
            for(j = k; j > k - i + 1; j--)
                document.write(j + "*");
                  
            document.write(j + "<br>");
        }
    }
}
  
// Driver code
var n = 5;
  
printPattern(n);
  
// This code is contributed by akshitsaxenaa09
  
</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 neveropen and would like to contribute, you can also write an article using contribute.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the neveropen 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.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments