Program for Stair Case Patterns

Programs to print following pattern.

Examples:

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



Output:


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

Program to print following pattern.

Examples:

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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)
?>

chevron_right



Output:

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

Program to print following pattern.

Examples:

Input : 6
Output :

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.*/

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output:


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

Program to print following pattern.

Examples:

Input : 6
Output :

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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)
?>

chevron_right



Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.