Skip to content
Related Articles

Related Articles

Program for Stair Case Patterns
  • Last Updated : 06 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

Programs to print following pattern.
Examples: 
 

Input : 6
Output :
* *
* *
* * * *
* * * *
* * * * * *
* * * * * *

 

 

C++




// CPP program to print stair case pattern.
#include <iostream>
 
using namespace std;
 
// Function definition
void pattern(int n)
{
    // for loop for rows
    for (int i = 1; i <= n; i++) {
 
        // conditional operator
        int k = (i % 2 != 0) ? i + 1 : i;
 
        // according to value of k carry
        // out further operation
        for (int j = 0; j < k; j++)
            cout << " * ";
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int n = 6;
    pattern(n);
    return 0;
}

Java




// Java program to print
// stair case pattern.
import java.io.*;
 
 
class GFG {
     
    // Function definition
    static void pattern(int n)
    {
        // for loop for rows
        for (int i = 1; i <= n; i++) {
     
            // conditional operator
            int k ;
             
            if(i % 2 != 0)
                k = i + 1 ;
            else
                k = i ;
     
            // according to value of k carry
            // out further operation
            for (int j = 0; j < k; j++)
                System.out.print(" * ");
            System.out.println();
        }
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 6;
        pattern(n);
    }
}
 
/*This code is contributed by Nikita Tiwari.*/

Python3




# Python3 program to print stair case pattern.
 
# Function definition
def pattern(n):
 
    # for loop for rows
    for i in range(1, n + 1):
         
        # conditional operator
        k = i + 1 if (i % 2 != 0) else i
 
        # according to value of k carry
        # out further operation
        for j in range(0, k):
            if j == k - 1:
                print(" * ")
            else:
                print(" * ", end = " ")
             
# Driver code
n = 6
pattern(n)
 
# This code is contributed by Smitha Dinesh Semwal

C#




// C# program to print stair case pattern.
using System;
  
class GFG {
      
    // Function definition
    static void pattern(int n)
    {
        // for loop for rows
        for (int i = 1; i <= n; i++) {
      
            // conditional operator
            int k ;
              
            if(i % 2 != 0)
                k = i + 1 ;
            else
                k = i ;
      
            // according to value of k carry
            // out further operation
            for (int j = 0; j < k; j++)
                Console.Write(" * ");
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main()
    {
        int n = 6;
        pattern(n);
    }
}
  
// This code is contributed by nitin mittal.

PHP




<?php
// PHP program to print
// stair case pattern.
 
// Function definition
function pattern($n)
{
    // for loop for rows
    for ($i = 1; $i <= $n; $i++)
    {
 
        // conditional operator
        $k = ($i % 2 != 0) ?
              $i + 1 : $i;
 
        // according to value
        // of k carry out
        // further operation
        for ($j = 0; $j < $k; $j++)
            echo " * ";
            echo"\n";
    }
}
 
// Driver code
$n = 6;
pattern($n);
     
// This code is contributed
// by anuj_67.
?>

Javascript




<script>
// Javascript program to prlet
// stair case pattern.
 
    // Function definition
    function pattern( n)
    {
     
        // for loop for rows
        for ( i = 1; i <= n; i++) {
 
            // conditional operator
            let k;
 
            if (i % 2 != 0)
                k = i + 1;
            else
                k = i;
 
            // according to value of k carry
            // out further operation
            for ( j = 0; j < k; j++)
                document.write(" * ");
            document.write("<br/>");
        }
    }
 
    // Driver code
      
    let n = 6;
    pattern(n);
 
// This code is contributed by gauravrajput1                   
</script>

Output: 
 

* *
* *
* * * *
* * * *
* * * * * *
* * * * * *

Program to print following pattern.
Examples: 
 



Input : 6
Output :
    * * 
    * * 
  * * * * 
  * * * * 
* * * * * * 
* * * * * * 

 

 

C++




// C++ Program for Stair Case Patterns
#include <iostream>
 
using namespace std;
 
// Function definition
void pattern(int n)
{
    // for loop for rows
    for (int i = 1; i <= n; i++) {
 
        // conditional operator
        int k = (i % 2 != 0) ? i + 1 : i;
 
        // for loop for printing spaces
        for (int g = n; g > k; g--)
            cout << " ";
 
        // according to value of k carry
        // out further operation
        for (int j = 0; j < k; j++)
            cout << "* ";
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int n = 6;
    pattern(n);
    return 0;
}

Java




// Java code for above
// Stair Case Patterns
import java.io.*;
 
class GFG {
     
    // Function definition
    static void pattern(int n)
    {
        // for loop for rows
        for (int i = 1; i <= n; i++) {
     
            // conditional operator
            int k;
             
            if(i % 2 != 0)
                k = i + 1 ;
            else
                k = i;
     
            // for loop for printing spaces
            for (int g = n; g > k; g--)
                System.out.print(" ");
     
            // according to value of k carry
            // out further operation
            for (int j = 0; j < k; j++)
                System.out.print("* ");
            System.out.println();
        }
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 6;
        pattern(n);
    }
}
 
 
/*This code is contributed by Nikita Tiwari.*/

Python3




# Python 3 program to
# print above pattern
 
# Function definition
def pattern(n) :
     
    # for loop for rows
    for i in range(1,n+1) :
         
        # conditional operator
        if(i % 2 != 0) :
             
            k = i + 1
        else :
            k = i
 
        # for loop for printing spaces
        for g in range(n ,k,-1) :
             
            print(" ",end="")
 
        # according to value of k carry
        # out further operation
        for j in range( 0, k):
            print("* ",end="")
        print()
         
 
# Driver Code
n = 6
pattern(n)
 
#This code is contributed by Nikita Tiwari.

C#




// C# code for above
// Stair Case Patterns
using System;
 
class GFG
{
 
// Function definition
static void pattern(int n)
{
    // for loop for rows
    for (int i = 1; i <= n; i++)
    {
 
        // conditional operator
        int k;
         
        if(i % 2 != 0)
            k = i + 1 ;
        else
            k = i;
 
        // for loop for
        // printing spaces
        for (int g = n; g > k; g--)
            Console.Write(" ");
 
        // according to value of
        // k carry out further
        // operation
        for (int j = 0; j < k; j++)
            Console.Write("* ");
        Console.WriteLine();
    }
}
 
// Driver code
public static void Main()
{
    int n = 6;
    pattern(n);
}
}
 
// This code is contributed by anuj_67.

PHP




<?php
// PHP code for above
// Stair Case Pattern
 
// Function definition
function pattern($n)
{
    // for loop for rows
    for ($i = 1; $i <= $n; $i++)
    {
 
        // conditional operator
        $k = ($i % 2 != 0) ? $i + 1 : $i;
 
        // for loop for printing spaces
        for ($g = $n; $g > $k; $g--)
            echo " ";
 
        // according to value of k carry
        // out further operation
        for ($j = 0; $j < $k; $j++)
            echo " *";
        echo "\n";
    }
}
 
// Driver code
$n = 6;
pattern($n);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>

Javascript




<script>
 
// JavaScript code for above
// Stair Case Patterns
 
    // Function definition
    function pattern(n) {
        // for loop for rows
        for (i = 1; i <= n; i++) {
 
            // conditional operator
            var k;
 
            if (i % 2 != 0)
                k = i + 1;
            else
                k = i;
 
            // for loop for printing spaces
            for (g = n; g > k; g--)
                document.write(" ");
 
            // according to value of k carry
            // out further operation
            for (j = 0; j < k; j++)
                document.write("* ");
            document.write("<br/>");
        }
    }
 
    // Driver code
     
        var n = 6;
        pattern(n);
 
// This code is contributed by aashish1995
 
</script>

Output: 
 

    * * 
    * * 
  * * * * 
  * * * * 
* * * * * * 
* * * * * * 

Program to print following pattern.
Examples: 
 

Input : 6
Output :

* * * * * *
* * * * * *
* * * *
* * * *
* *
* *

 

 

C++




// C++ program for above
// Stair Case Patterns
#include <iostream>
 
using namespace std;
 
// Function definition
void pattern(int n)
{
    // for loop for rows
    for (int i = n; i > 0; i--) {
 
        // conditional operator
        int k = (i % 2 != 0) ? i + 1 : i;
 
        // according to value of k carry
        // out further operation
        for (int j = 0; j < k; j++)
            cout << " * ";
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int n = 6;
    pattern(n);
    return 0;
}

Java




// java Program for print the above pattern
import java.io.*;
 
class GFG {
     
    // Function definition
    static void pattern(int n)
    {
        // for loop for rows
        for (int i = n; i > 0; i--) {
     
            // conditional operator
            int k;
            if (i % 2 != 0)
                k = i + 1;
            else
                k = i;
     
            // according to value of k carry
            // out further operation
            for (int j = 0; j < k; j++)
                System.out.print(" * ");
            System.out.println();
        }
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 6;
        pattern(n);
    }
}
 
 
/* This code is contributed by Nikita Tiwari.*/

Python 3




# Python 3 Program for print the above pattern
# Function definition
def pattern(n) :
     
    # for loop for rows
    for i in range(n,0,-1) :
         
        # conditional operator
        if(i % 2 != 0) :
             
            k = i + 1
        else :
            k = i
 
        # for loop for printing spaces
        for g in range(n, k) :
            print(" ",end="")
 
        # according to value of k carry
        # out further operation
        for j in range(0, k) :
 
            print(" * ",end="")
        print()
         
 
# Driver code
n = 6
pattern(n)
 
 
#This code is contributed by Nikita Tiwari.

C#




// C# Program for print
// above stair case pattern
using System;
 
class GFG
{
     
// Function definition
static void pattern(int n)
{
    // for loop for rows
    for (int i = n; i > 0; i--)
    {
 
        // conditional operator
        int k;
        if (i % 2 != 0)
            k = i + 1;
        else
            k = i;
 
        // according to value
        // of k carry out
        // further operation
        for (int j = 0; j < k; j++)
        Console.Write(" * ");
        Console.WriteLine();
    }
}
 
// Driver code
public static void Main()
{
    int n = 6;
    pattern(n);
}
}
 
// This code is contributed
// by anuj_67.

PHP




<?php
// PHP Program for print
// above stair case pattern
 
// Function definition
function pattern($n)
{
    // for loop for rows
    for ($i = $n; $i > 0; $i--)
    {
 
        // conditional operator
        $k = ($i % 2 != 0) ?
                    $i + 1 : $i;
 
        // according to value
        // of k carry out
        // further operation
        for ($j = 0; $j < $k; $j++)
            echo("* ");
        echo("\n");
    }
}
 
// Driver code
$n = 6;
pattern($n);
 
// This code is contributed
// by Smitha
 
?>

Javascript




<script>
// javascript Program for prvar the above pattern
 
    // Function definition
    function pattern(n) {
        // for loop for rows
        for (var i = n; i > 0; i--) {
 
            // conditional operator
            var k;
            if (i % 2 != 0)
                k = i + 1;
            else
                k = i;
 
            // according to value of k carry
            // out further operation
            for (var j = 0; j < k; j++)
                document.write(" * ");
            document.write("<br/>");
        }
    }
 
    // Driver code
     
        var n = 6;
        pattern(n);
 
// This code contributed by gauravrajput1
</script>

Output: 
 

* * * * * *
* * * * * *
* * * *
* * * *
* *
* *

Program to print following pattern.
Examples: 
 

Input : 6
Output :

* * * * * * 
* * * * * * 
  * * * * 
  * * * * 
    * * 
    * * 

 

C++




// C++ program to
// print above pattern.
#include <iostream>
 
using namespace std;
 
// Function definition
void pattern(int n)
{
    // for loop for rows
    for (int i = n; i > 0; i--)
    {
 
        // conditional operator
        int k = (i % 2 != 0) ?
                    i + 1 : i;
 
        // for loop for printing spaces
        for (int g = k; g < n; g++)
            cout << " ";
 
        // according to value of k carry
        // out further operation
        for (int j = 0; j < k; j++)
            cout << "* ";
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int n = 6;
    pattern(n);
    return 0;
}

Java




// Java program to
// print above pattern.
import java.io.*;
 
class GFG
{
     
    // Function definition
    static void pattern(int n)
    {
         
    // for loop for rows
    for (int i = n; i > 0; i--)
    {
     
        // conditional operator
        int k = (i % 2 != 0) ?
                    i + 1 : i;
     
        // for loop for printing spaces
        for (int g = k; g < n; g++)
            System.out.print(" ");
     
        // according to value of k carry
        // out further operation
        for (int j = 0; j < k; j++)
            System.out.print("* ");
            System.out.println();
        }
    }
     
    // Driver code
    public static void main(String args[])
    {
        int n = 6;
        pattern(n);
    }
}
 
// This code is contributed
// by Nikita Tiwari

Python 3




# Python 3 program to
# print above pattern.
 
# Function definition
def pattern(n) :
     
    # for loop for rows
    for i in range(n, 0, -1) :
         
        # conditional operator
        if(i % 2 != 0) :
             
            k = i + 1
        else :
            k = i
 
        # for loop for printing spaces
        for g in range(k, n) :
             
            print(" ", end = "")
 
        # according to value of k carry
        # out further operation
        for j in range(0,k) :
            print("* ", end = "")
        print()
         
 
# Driver code
n = 6
pattern(n)
 
 
# This code is contributed
# by Nikita Tiwari.

C#




// C# program to
// print above pattern.
using System;
 
class GFG
{
     
    // Function definition
    static void pattern(int n)
    {
    // for loop for rows
    for (int i = n; i > 0; i--)
    {
     
        // conditional operator
        int k = (i % 2 != 0) ?
                    i + 1 : i;
     
        // for loop for printing spaces
        for (int g = k; g < n; g++)
            Console.Write(" ");
     
        // according to value of k carry
        // out further operation
        for (int j = 0; j < k; j++)
            Console.Write("* ");
            Console.WriteLine();
        }
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 6;
        pattern(n);
    }
}
 
// This code is contributed by ajit

PHP




<?php
// PHP program to
// print above pattern.
 
// Function definition
function pattern($n)
{
    // for loop for rows
    for ($i = $n; $i > 0; $i--)
    {
 
        // conditional operator
        $k = ($i % 2 != 0) ?
                    $i + 1 : $i;
 
        // for loop for
        // printing spaces
        for ($g = $k; $g < $n; $g++)
            echo " ";
 
        // according to value of k
        // carry out further operation
        for ($j = 0; $j < $k; $j++)
            echo "* ";
        echo "\n";
    }
}
 
// Driver code
$n = 6;
pattern($n);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>

Output: 
 

* * * * * * 
* * * * * * 
  * * * * 
  * * * * 
    * * 
    * * 

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :