Program for Stair Case Patterns

23 Nov, 2021
Programs to print following pattern.Examples: 
Input : 6
Output :
* *
* *
* * * *
* * * *
* * * * * *
* * * * * *
C++
Java
Python3
C#
PHP
Javascript
// CPP program to print stair case pattern.#include <iostream> using namespace std; // Function definitionvoid 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 codeint main(){    int n = 6;    pattern(n);    return 0;}
// 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 program to print stair case pattern. # Function definitiondef 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 coden = 6pattern(n) # This code is contributed by Smitha Dinesh Semwal
// 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 program to print// stair case pattern. // Function definitionfunction 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.?>
<script>// Javascript program to print// 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++
Java
Python3
C#
PHP
Javascript
// C++ Program for Stair Case Patterns#include <iostream> using namespace std; // Function definitionvoid 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 codeint main(){    int n = 6;    pattern(n);    return 0;}
// Java code for above// Stair Case Patternsimport 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.*/
# Python 3 program to# print above pattern # Function definitiondef 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 Coden = 6pattern(n) #This code is contributed by Nikita Tiwari.
// C# code for above// Stair Case Patternsusing System; class GFG{ // Function definitionstatic 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 codepublic static void Main(){    int n = 6;    pattern(n);}} // This code is contributed by anuj_67.
<?php// PHP code for above// Stair Case Pattern // Function definitionfunction 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)?>
<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++
Java
Python 3
C#
PHP
Javascript
// C++ program for above// Stair Case Patterns#include <iostream> using namespace std; // Function definitionvoid 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 codeint main(){    int n = 6;    pattern(n);    return 0;}
// java Program for print the above patternimport 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 Program for print the above pattern# Function definitiondef 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 coden = 6pattern(n)  #This code is contributed by Nikita Tiwari.
// C# Program for print// above stair case patternusing System; class GFG{     // Function definitionstatic 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 codepublic static void Main(){    int n = 6;    pattern(n);}} // This code is contributed// by anuj_67.
<?php// PHP Program for print// above stair case pattern // Function definitionfunction 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 ?>
<script>// javascript Program for print 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++
Java
Python3
C#
PHP
Javascript
// C++ program to// print above pattern.#include <iostream> using namespace std; // Function definitionvoid 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 codeint main(){    int n = 6;    pattern(n);    return 0;}
// 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 program to# print above pattern. # Function definitiondef 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 coden = 6pattern(n)  # This code is contributed# by Nikita Tiwari.
// 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 program to// print above pattern. // Function definitionfunction 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)?>
<script> // Javascript program to// print above pattern. // Function definitionfunction pattern(n){         // For loop for rows    for(i = n; i > 0; i--)    {                 // conditional operator        var k = (i % 2 != 0) ? i + 1 : i;         // For loop for printing spaces        for(g = k; g < n; g++)            document.write("  ");         // According to value of k carry        // out further operation        for(j = 0; j < k; j++)            document.write("*  ");                     document.write("<br/>");    }} // Driver codevar n = 6; pattern(n); // This code is contributed by aashish1995 </script>
Output: 
* * * * * * 
* * * * * * 
  * * * * 
  * * * * 
    * * 
    * * 
 
nitin mittal
jit_t
vt_m
Smitha Dinesh Semwal
Akanksha_Rai
GauravRajput1
aashish1995
ankita_saini
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Python Exception Handling
Python Try Except
Ternary Operator in Python
How JVM Works - JVM Architecture?
Python program to add two numbers
Variables in Java
Data types in Java
Difference between Abstract Class and Interface in Java