📜  楼梯案例模式程序

📅  最后修改于: 2022-05-13 01:57:59.459000             🧑  作者: Mango

楼梯案例模式程序

打印以下模式的程序。
例子:

Input : 6
Output :
* *
* *
* * * *
* * * *
* * * * * *
* * * * * *
C++
// CPP program to print stair case pattern.
#include 
 
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


Javascript


C++
// C++ Program for Stair Case Patterns
#include 
 
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
 $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


C++
// C++ program for above
// Stair Case Patterns
#include 
 
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
 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


C++
// C++ program to
// print above pattern.
#include 
 
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


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


Javascript


输出:

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

程序打印以下模式。
例子:

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

C++

// C++ Program for Stair Case Patterns
#include 
 
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

 $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


输出:

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

程序打印以下模式。
例子:

Input : 6
Output :

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

C++

// C++ program for above
// Stair Case Patterns
#include 
 
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.*/

Python3

# 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

 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


输出:

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

程序打印以下模式。
例子:

Input : 6
Output :

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

C++

// C++ program to
// print above pattern.
#include 
 
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

Python3

# 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

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

Javascript


输出:

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