📜  使用递归打印金字塔图案的程序

📅  最后修改于: 2021-05-31 20:50:48             🧑  作者: Mango

本文旨在提供一种模式打印的递归实现。

  • 简单的三角形图案:
C++
// C++ code to demonstrate star pattern
#include 
using namespace std;
 
// function to print a row
void printn(int num)
{
    // base case
    if (num == 0)
        return;
    cout << "* ";
 
    // recursively calling printn()
    printn(num - 1);
}
 
// function to print the pattern
void pattern(int n, int i)
{
    // base case
    if (n == 0)
        return;
    printn(i);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, i + 1);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, 1);
    return 0;
}


Java
// Java code to demonstrate star pattern
import java.io.*;
 
class GFG
{
     
// function to print a row
static void printn(int num)
{
    // base case
    if (num == 0)
        return;
    System.out.print ("* ");
 
    // recursively calling printn()
    printn(num - 1);
}
 
// function to print the pattern
static void pattern(int n, int i)
{
    // base case
    if (n == 0)
        return;
    printn(i);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, i + 1);
}
 
// Driver code
public static void main (String[] args)
{
 
    int n = 5;
    pattern(n, 1);
}
}
 
// This code is contributed by ajit.


Python3
# Python3 code to demonstrate star pattern
 
# function to print a row
def printn(num):
     
    # base case
    if (num == 0):
        return
    print("*", end = " ")
 
    # recursively calling printn()
    printn(num - 1)
 
# function to print the pattern
def pattern(n, i):
     
    # base case
    if (n == 0):
        return
    printn(i)
    print("\n", end = "")
     
    # recursively calling pattern()
    pattern(n - 1, i + 1)
 
# Driver Code
if __name__ == '__main__':
    n = 5
    pattern(n, 1)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# code to demonstrate star pattern
using System;
 
class GFG
{
         
// function to print a row
static void printn(int num)
{
    // base case
    if (num == 0)
        return;
    Console.Write("* ");
 
    // recursively calling printn()
    printn(num - 1);
}
 
// function to print the pattern
static void pattern(int n, int i)
{
    // base case
    if (n == 0)
        return;
    printn(i);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, i + 1);
}
 
// Driver code
static public void Main ()
{
    int n = 5;
    pattern(n, 1);
}
}
 
// This code is contributed by akt_mit.


PHP


Javascript


C++
// C++ code to demonstrate star pattern
 
#include 
using namespace std;
 
// function to print spaces
void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    cout << " "
         << " ";
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    cout << "* ";
 
    // recursively calling print_asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, n);
    return 0;
}


Java
// Java code to demonstrate star pattern
import java.util.*;
 
class GFG
{
 
    // function to print spaces
    static void print_space(int space)
    {
        // base case
        if (space == 0)
        {
            return;
        }
        System.out.print(" " + " ");
 
        // recursively calling print_space()
        print_space(space - 1);
    }
 
    // function to print asterisks
    static void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
        {
            return;
        }
        System.out.print("* ");
 
        // recursively calling print_asterisk()
        print_asterisk(asterisk - 1);
    }
 
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
        {
            return;
        }
        print_space(n - 1);
        print_asterisk(num - n + 1);
        System.out.println();
 
        // recursively calling pattern()
        pattern(n - 1, num);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        pattern(n, n);
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 code to demonstrate star pattern
 
# function to prspaces
def print_space(space):
     
    # base case
    if (space == 0):
        return;
     
    print(" ", end=" ");
 
    # recursively calling print_space()
    print_space(space - 1);
 
# function to prasterisks
def print_asterisk(asterisk):
     
    # base case
    if (asterisk == 0):
        return;
     
    print("*", end =" ");
 
    # recursively calling print_asterisk()
    print_asterisk(asterisk - 1);
 
# function to print the pattern
def pattern(n, num):
     
    # base case
    if (n == 0):
        return;
     
    print_space(n - 1);
    print_asterisk(num - n + 1);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
# Driver code
if __name__ == '__main__':
    n = 5;
    pattern(n, n);
     
# This code is contributed by 29AjayKumar


C#
// C# code to demonstrate star pattern
using System;
 
class GFG
{
     
// function to print spaces
static void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    Console.Write(" " + " ");
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
static void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    Console.Write("* ");
 
    // recursively calling print_asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void Main()
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Akanksha Rai


PHP


C++
// C++ code to demonstrate star pattern
 
#include 
using namespace std;
 
// function to print spaces
void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    cout << " ";
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    cout << "* ";
 
    // recursively calling asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, n);
    return 0;
}


Java
// Java code to demonstrate star pattern
import java.util.*;
 
class GFG
{
 
// function to print spaces
static void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    System.out.print(" ");
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
static void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    System.out.print("* ");
 
    // recursively calling asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    System.out.println("");
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 code to demonstrate star pattern
 
# function to print spaces
def print_space(space):
     
    # base case
    if (space == 0):
        return;
    print(" ", end = "");
 
    # recursively calling print_space()
    print_space(space - 1);
 
# function to print asterisks
def print_asterisk(asterisk):
     
    # base case
    if(asterisk == 0):
        return;
    print("* ", end = "");
 
    # recursively calling asterisk()
    print_asterisk(asterisk - 1);
 
# function to print the pattern
def pattern(n, num):
     
    # base case
    if (n == 0):
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    print("");
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
# Driver Code
n = 5;
pattern(n, n);
 
# This code contributed by PrinciRaj1992


C#
// C# code to demonstrate star pattern
using System;
 
class GFG
{
 
// function to print spaces
static void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    Console.Write(" ");
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
static void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    Console.Write("* ");
 
    // recursively calling asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    Console.WriteLine("");
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by 29AjayKumar


PHP


C++
// C++ code to demonstrate printing pattern of numbers
 
#include 
using namespace std;
 
// function to print a row
void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    cout << val << " ";
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
int main()
{
    int n = 5;
    pattern(n, n);
}


Java
// Java code to demonstrate printing
// pattern of numbers
class GFG
{
     
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    System.out.print(val + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Code_Mech.


Python3
# Python3 code to demonstrate printing
# pattern of numbers
 
# function to print a row
def print_row(no, val):
     
    # base case
    if (no == 0):
        return;
    print(val , end=" ");
 
    # recursively calling print_row()
    print_row(no - 1, val);
 
# function to print the pattern
def pattern(n, num):
    # base case
    if (n == 0):
        return;
    print_row(num - n + 1, num - n + 1);
    print("");
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
# Driver Code
n = 5;
pattern(n, n);
 
# This code contributed by PrinciRaj1992


C#
// C# code to demonstrate printing
// pattern of numbers
using System;
 
class GFG
{
     
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    Console.Write(val + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver Code
public static void Main()
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Akanksha Rai


PHP


Javascript


C++
// C++ code to demonstrate printing pattern of numbers
 
#include 
using namespace std;
 
// function to print a row
int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    cout << ct << "\t";
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
}
 
// function to print the pattern
void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, 1, 1);
}


Java
// Java code to demonstrate
// printing pattern of numbers
import java.io.*;
 
class GFG
{
     
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
            System.out.print(ct + "  ");
 
    // recursively calling print_row()
    return print_row(ct + 1, num - 1);
}
 
// function to print the pattern
static void pattern(int n,
                    int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
            System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by Tushil


Python3
# Python3 code to demonstrate
# printing pattern of numbers
 
# function to pra row
def print_row(ct, num):
     
    # base case
    if (num == 0):
        return ct;
    print(ct, end = " ");
 
    # recursively calling print_row()
    return print_row(ct + 1, num - 1);
 
# function to print the pattern
def pattern(n, count, num):
     
    # base case
    if (n == 0):
        return;
    count = print_row(count, num);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, count, num + 1);
 
# Driver Code
if __name__ == '__main__':
    n = 5;
    pattern(n, 1, 1);
 
# This code is contributed by 29AjayKumar


C#
// C# code to demonstrate
// printing pattern of numbers
using System;
 
class GFG
{
 
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    Console.Write (ct + "  ");
 
    // recursively calling print_row()
    return print_row(ct + 1, num - 1);
}
 
// function to print the pattern
static void pattern(int n,
                    int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver Code
static public void Main ()
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by ajit


Javascript


C++
// C++ code to demonstrate printing pattern of alphabets
 
#include 
using namespace std;
 
// function to print a row
void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    cout << (char)(val + 64) << " ";
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
int main()
{
    int n = 5;
    pattern(n, n);
}


Java
// Java code to demonstrate printing pattern of alphabets
class GFG
{
 
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    System.out.print((char)(val + 64) + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 code to demonstrate printing pattern of alphabets
 
 
# function to pra row
def print_row(no, val):
    # base case
    if (no == 0):
        return;
    print(chr(val + 64), end=" ");
 
    # recursively calling print_row()
    print_row(no - 1, val);
 
 
# function to print the pattern
def pattern(n, num):
    # base case
    if (n == 0):
        return;
    print_row(num - n + 1, num - n + 1);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
 
# Driver code
if __name__ == '__main__':
    n = 5;
    pattern(n, n);
 
# This code is contributed by 29AjayKumar


C#
// C# code to demonstrate printing pattern of alphabets
using System;
 
class GFG
{
 
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    Console.Write((char)(val + 64) + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by 29AjayKumar


C++
#include 
using namespace std;
 
// function to print a row
int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    cout << (char)(ct + 64) << " ";
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
}
 
// function to print the pattern
void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, 1, 1);
}


Java
class GFG
{
 
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    System.out.print((char)(ct + 64) + " ");
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
    return num + ct;
     
}
 
// function to print the pattern
static void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# function to print row
def print_row(ct, num):
     
    # base case
    if (num == 0):
        return ct;
    print(chr(ct + 64), end=" ");
 
    # recursively calling print_row()
    print_row(ct + 1, num - 1);
    return num + ct;
 
# function to print the pattern
def pattern(n, count, num):
 
    # base case
    if (n == 0):
        return;
    count = print_row(count, num);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, count, num + 1);
 
# Driver code
if __name__ == '__main__':
    n = 5;
    pattern(n, 1, 1);
 
# This code is contributed by Rajput-Ji


C#
using System;
 
class GFG
{
 
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    Console.Write((char)(ct + 64) + " ");
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
    return num + ct;
 
}
 
// function to print the pattern
static void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
* 
* * 
* * * 
* * * * 
* * * * *
  • 180度旋转后:

C++

// C++ code to demonstrate star pattern
 
#include 
using namespace std;
 
// function to print spaces
void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    cout << " "
         << " ";
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    cout << "* ";
 
    // recursively calling print_asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, n);
    return 0;
}

Java

// Java code to demonstrate star pattern
import java.util.*;
 
class GFG
{
 
    // function to print spaces
    static void print_space(int space)
    {
        // base case
        if (space == 0)
        {
            return;
        }
        System.out.print(" " + " ");
 
        // recursively calling print_space()
        print_space(space - 1);
    }
 
    // function to print asterisks
    static void print_asterisk(int asterisk)
    {
        // base case
        if (asterisk == 0)
        {
            return;
        }
        System.out.print("* ");
 
        // recursively calling print_asterisk()
        print_asterisk(asterisk - 1);
    }
 
    // function to print the pattern
    static void pattern(int n, int num)
    {
        // base case
        if (n == 0)
        {
            return;
        }
        print_space(n - 1);
        print_asterisk(num - n + 1);
        System.out.println();
 
        // recursively calling pattern()
        pattern(n - 1, num);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        pattern(n, n);
    }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 code to demonstrate star pattern
 
# function to prspaces
def print_space(space):
     
    # base case
    if (space == 0):
        return;
     
    print(" ", end=" ");
 
    # recursively calling print_space()
    print_space(space - 1);
 
# function to prasterisks
def print_asterisk(asterisk):
     
    # base case
    if (asterisk == 0):
        return;
     
    print("*", end =" ");
 
    # recursively calling print_asterisk()
    print_asterisk(asterisk - 1);
 
# function to print the pattern
def pattern(n, num):
     
    # base case
    if (n == 0):
        return;
     
    print_space(n - 1);
    print_asterisk(num - n + 1);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
# Driver code
if __name__ == '__main__':
    n = 5;
    pattern(n, n);
     
# This code is contributed by 29AjayKumar

C#

// C# code to demonstrate star pattern
using System;
 
class GFG
{
     
// function to print spaces
static void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    Console.Write(" " + " ");
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
static void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    Console.Write("* ");
 
    // recursively calling print_asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void Main()
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Akanksha Rai

的PHP


输出:
* 
      * * 
    * * * 
  * * * * 
* * * * *
  • 印刷金字塔:

C++

// C++ code to demonstrate star pattern
 
#include 
using namespace std;
 
// function to print spaces
void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    cout << " ";
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    cout << "* ";
 
    // recursively calling asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, n);
    return 0;
}

Java

// Java code to demonstrate star pattern
import java.util.*;
 
class GFG
{
 
// function to print spaces
static void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    System.out.print(" ");
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
static void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    System.out.print("* ");
 
    // recursively calling asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    System.out.println("");
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 code to demonstrate star pattern
 
# function to print spaces
def print_space(space):
     
    # base case
    if (space == 0):
        return;
    print(" ", end = "");
 
    # recursively calling print_space()
    print_space(space - 1);
 
# function to print asterisks
def print_asterisk(asterisk):
     
    # base case
    if(asterisk == 0):
        return;
    print("* ", end = "");
 
    # recursively calling asterisk()
    print_asterisk(asterisk - 1);
 
# function to print the pattern
def pattern(n, num):
     
    # base case
    if (n == 0):
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    print("");
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
# Driver Code
n = 5;
pattern(n, n);
 
# This code contributed by PrinciRaj1992

C#

// C# code to demonstrate star pattern
using System;
 
class GFG
{
 
// function to print spaces
static void print_space(int space)
{
    // base case
    if (space == 0)
        return;
    Console.Write(" ");
 
    // recursively calling print_space()
    print_space(space - 1);
}
 
// function to print asterisks
static void print_asterisk(int asterisk)
{
    // base case
    if (asterisk == 0)
        return;
    Console.Write("* ");
 
    // recursively calling asterisk()
    print_asterisk(asterisk - 1);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_space(n - 1);
    print_asterisk(num - n + 1);
    Console.WriteLine("");
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by 29AjayKumar

的PHP


输出:
* 
   * * 
  * * * 
 * * * * 
* * * * *
  • 数字模式

C++

// C++ code to demonstrate printing pattern of numbers
 
#include 
using namespace std;
 
// function to print a row
void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    cout << val << " ";
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
int main()
{
    int n = 5;
    pattern(n, n);
}

Java

// Java code to demonstrate printing
// pattern of numbers
class GFG
{
     
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    System.out.print(val + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Code_Mech.

Python3

# Python3 code to demonstrate printing
# pattern of numbers
 
# function to print a row
def print_row(no, val):
     
    # base case
    if (no == 0):
        return;
    print(val , end=" ");
 
    # recursively calling print_row()
    print_row(no - 1, val);
 
# function to print the pattern
def pattern(n, num):
    # base case
    if (n == 0):
        return;
    print_row(num - n + 1, num - n + 1);
    print("");
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
# Driver Code
n = 5;
pattern(n, n);
 
# This code contributed by PrinciRaj1992

C#

// C# code to demonstrate printing
// pattern of numbers
using System;
 
class GFG
{
     
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    Console.Write(val + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver Code
public static void Main()
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Akanksha Rai

的PHP


Java脚本


输出:
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5
  • 没有重新分配的号码

C++

// C++ code to demonstrate printing pattern of numbers
 
#include 
using namespace std;
 
// function to print a row
int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    cout << ct << "\t";
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
}
 
// function to print the pattern
void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, 1, 1);
}

Java

// Java code to demonstrate
// printing pattern of numbers
import java.io.*;
 
class GFG
{
     
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
            System.out.print(ct + "  ");
 
    // recursively calling print_row()
    return print_row(ct + 1, num - 1);
}
 
// function to print the pattern
static void pattern(int n,
                    int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
            System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by Tushil

Python3

# Python3 code to demonstrate
# printing pattern of numbers
 
# function to pra row
def print_row(ct, num):
     
    # base case
    if (num == 0):
        return ct;
    print(ct, end = " ");
 
    # recursively calling print_row()
    return print_row(ct + 1, num - 1);
 
# function to print the pattern
def pattern(n, count, num):
     
    # base case
    if (n == 0):
        return;
    count = print_row(count, num);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, count, num + 1);
 
# Driver Code
if __name__ == '__main__':
    n = 5;
    pattern(n, 1, 1);
 
# This code is contributed by 29AjayKumar

C#

// C# code to demonstrate
// printing pattern of numbers
using System;
 
class GFG
{
 
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    Console.Write (ct + "  ");
 
    // recursively calling print_row()
    return print_row(ct + 1, num - 1);
}
 
// function to print the pattern
static void pattern(int n,
                    int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver Code
static public void Main ()
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by ajit

Java脚本


输出:
1    
2    3    
4    5    6    
7    8    9    10    
11    12    13    14    15
  • 字符模式:

C++

// C++ code to demonstrate printing pattern of alphabets
 
#include 
using namespace std;
 
// function to print a row
void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    cout << (char)(val + 64) << " ";
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
int main()
{
    int n = 5;
    pattern(n, n);
}

Java

// Java code to demonstrate printing pattern of alphabets
class GFG
{
 
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    System.out.print((char)(val + 64) + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 code to demonstrate printing pattern of alphabets
 
 
# function to pra row
def print_row(no, val):
    # base case
    if (no == 0):
        return;
    print(chr(val + 64), end=" ");
 
    # recursively calling print_row()
    print_row(no - 1, val);
 
 
# function to print the pattern
def pattern(n, num):
    # base case
    if (n == 0):
        return;
    print_row(num - n + 1, num - n + 1);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, num);
 
 
# Driver code
if __name__ == '__main__':
    n = 5;
    pattern(n, n);
 
# This code is contributed by 29AjayKumar

C#

// C# code to demonstrate printing pattern of alphabets
using System;
 
class GFG
{
 
// function to print a row
static void print_row(int no, int val)
{
    // base case
    if (no == 0)
        return;
    Console.Write((char)(val + 64) + " ");
 
    // recursively calling print_row()
    print_row(no - 1, val);
}
 
// function to print the pattern
static void pattern(int n, int num)
{
    // base case
    if (n == 0)
        return;
    print_row(num - n + 1, num - n + 1);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, num);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    pattern(n, n);
}
}
 
// This code is contributed by 29AjayKumar
输出:
A 
B B 
C C C 
D D D D 
E E E E E
  • 连续字符模式:

C++

#include 
using namespace std;
 
// function to print a row
int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    cout << (char)(ct + 64) << " ";
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
}
 
// function to print the pattern
void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    cout << endl;
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// driver function
int main()
{
    int n = 5;
    pattern(n, 1, 1);
}

Java

class GFG
{
 
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    System.out.print((char)(ct + 64) + " ");
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
    return num + ct;
     
}
 
// function to print the pattern
static void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    System.out.println();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by 29AjayKumar

Python3

# function to print row
def print_row(ct, num):
     
    # base case
    if (num == 0):
        return ct;
    print(chr(ct + 64), end=" ");
 
    # recursively calling print_row()
    print_row(ct + 1, num - 1);
    return num + ct;
 
# function to print the pattern
def pattern(n, count, num):
 
    # base case
    if (n == 0):
        return;
    count = print_row(count, num);
    print();
 
    # recursively calling pattern()
    pattern(n - 1, count, num + 1);
 
# Driver code
if __name__ == '__main__':
    n = 5;
    pattern(n, 1, 1);
 
# This code is contributed by Rajput-Ji

C#

using System;
 
class GFG
{
 
// function to print a row
static int print_row(int ct, int num)
{
    // base case
    if (num == 0)
        return ct;
    Console.Write((char)(ct + 64) + " ");
 
    // recursively calling print_row()
    print_row(ct + 1, num - 1);
    return num + ct;
 
}
 
// function to print the pattern
static void pattern(int n, int count, int num)
{
    // base case
    if (n == 0)
        return;
    count = print_row(count, num);
    Console.WriteLine();
 
    // recursively calling pattern()
    pattern(n - 1, count, num + 1);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    pattern(n, 1, 1);
}
}
 
// This code is contributed by 29AjayKumar

Java脚本


输出:
A 
B C 
D E F 
G H I J 
K L M N O
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”