本文旨在提供一种模式打印的递归实现。
- 模式1:
例子:
Input: 5
Output:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
- 实施方式:
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the pattern
void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
cout << endl;
// recursively calling pattern()
pattern(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Program to print the given pattern
import java.util.*;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the pattern
static void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
System.out.print("\n");
// 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 PrinciRaj1992
Python3
# Program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("* ", end = "");
print_asterisk(asterisk - 1);
def print_space(space):
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the pattern
def pattern(n, num):
# base case
if (n == 0):
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
print("");
# recursively calling pattern()
pattern(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Princi Singh
C#
// C# Program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print the pattern
static void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
Console.Write("\n");
// 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 PrinciRaj1992
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the pattern
void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * 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 program to print the given pattern
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the pattern
static void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * 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 Princi Singh
Python3
# Program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("*", end = " ");
print_asterisk(asterisk - 1);
def print_space(space):
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the pattern
def pattern(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print_space(2 * 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 Princi Singh
C#
// C# program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print the pattern
static void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * 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 Rajput-Ji
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Program to print the given pattern
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
System.out.println();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
System.out.println();
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(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 program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("*", end = " ");
print_asterisk(asterisk - 1);
# function to print the
# upper-half of the pattern
def pattern_upper(n, num):
# base case
if (n == 0):
return;
print_asterisk(n);
print();
# recursively calling pattern_upper()
pattern_upper(n - 1, num);
# function to print the
# lower-half of the pattern
def pattern_lower(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print();
# recursively calling pattern_lower()
pattern_lower(n - 1, num);
# function to print the pattern
def pattern(n, num):
pattern_upper(n, num);
pattern_lower(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Rajput-Ji
C#
// Program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
Console.WriteLine();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
Console.WriteLine();
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Java Program to print the given pattern
import java.util.*;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
System.out.print("\n");
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
System.out.print("\n");
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
public static void main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("* ", end = "");
print_asterisk(asterisk - 1);
def print_space(space):
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the upper-half of the pattern
def pattern_upper(n, num):
# base case
if (n == 0):
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
print();
# recursively calling pattern_upper()
pattern_upper(n - 1, num);
# function to print the lower-half of the pattern
def pattern_lower(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
print();
# recursively calling pattern_lower()
pattern_lower(n - 1, num);
# function to print the pattern
def pattern(n, num):
pattern_upper(n, num);
pattern_lower(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Rajput Ji
C#
// C# Program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
Console.Write("\n");
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
Console.Write("\n");
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by 29AjayKumar
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
// base case
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
cout << endl;
// recursively calling pattern_lower(
pattern_lower(n - 1, num);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Java program to print the given pattern
class GFG
{
static void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
// base case
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
System.out.println();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
System.out.println();
// recursively calling pattern_lower(
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(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 program to print the given pattern
def print_asterisk(asterisk):
# base case
if (asterisk == 0):
return;
print("* ", end = "");
print_asterisk(asterisk - 1);
def print_space(space):
# base case
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the
# upper-half of the pattern
def pattern_upper(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
print();
# recursively calling pattern_upper()
pattern_upper(n - 1, num);
# function to print the lower-half
# of the pattern
def pattern_lower(n, num):
if (n == 0):
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
print();
# recursively calling pattern_lower(
pattern_lower(n - 1, num);
# function to print the pattern
def pattern(n, num):
pattern_upper(n, num);
pattern_lower(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Princi Raj
C#
// C# program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
// base case
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print
// the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
Console.WriteLine();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
Console.WriteLine();
// recursively calling pattern_lower(
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by Princi Singh
- 输出:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
- 模式2:
例子:
Input: 5
Output:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
- 实现方式:
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the pattern
void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * 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 program to print the given pattern
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the pattern
static void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * 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 Princi Singh
Python3
# Program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("*", end = " ");
print_asterisk(asterisk - 1);
def print_space(space):
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the pattern
def pattern(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print_space(2 * 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 Princi Singh
C#
// C# program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print the pattern
static void pattern(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * 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 Rajput-Ji
- 输出:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
- 模式3:
例子:
Input: 5
Output:
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
- 实施方式:
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Program to print the given pattern
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
System.out.println();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
System.out.println();
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(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 program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("*", end = " ");
print_asterisk(asterisk - 1);
# function to print the
# upper-half of the pattern
def pattern_upper(n, num):
# base case
if (n == 0):
return;
print_asterisk(n);
print();
# recursively calling pattern_upper()
pattern_upper(n - 1, num);
# function to print the
# lower-half of the pattern
def pattern_lower(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print();
# recursively calling pattern_lower()
pattern_lower(n - 1, num);
# function to print the pattern
def pattern(n, num):
pattern_upper(n, num);
pattern_lower(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Rajput-Ji
C#
// Program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
Console.WriteLine();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
Console.WriteLine();
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by 29AjayKumar
Java脚本
- 输出:
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
- 模式4:
例子:
Input: 5
Output:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
- 实施方式:
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Java Program to print the given pattern
import java.util.*;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
System.out.print("\n");
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
System.out.print("\n");
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
public static void main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to print the given pattern
def print_asterisk(asterisk):
if (asterisk == 0):
return;
print("* ", end = "");
print_asterisk(asterisk - 1);
def print_space(space):
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the upper-half of the pattern
def pattern_upper(n, num):
# base case
if (n == 0):
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
print();
# recursively calling pattern_upper()
pattern_upper(n - 1, num);
# function to print the lower-half of the pattern
def pattern_lower(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
print();
# recursively calling pattern_lower()
pattern_lower(n - 1, num);
# function to print the pattern
def pattern(n, num):
pattern_upper(n, num);
pattern_lower(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Rajput Ji
C#
// C# Program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
Console.Write("\n");
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
Console.Write("\n");
// recursively calling pattern_lower()
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by 29AjayKumar
- 输出:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
- 模式5:
例子:
Input: 5
Output:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
- 实施方式:
C++
// Program to print the given pattern
#include
using namespace std;
void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
cout << "* ";
print_asterisk(asterisk - 1);
}
void print_space(int space)
{
// base case
if (space == 0)
return;
cout << " "
<< " ";
print_space(space - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
cout << endl;
// recursively calling pattern_lower(
pattern_lower(n - 1, num);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// driver function
int main()
{
int n = 5;
pattern(n, n);
return 0;
}
Java
// Java program to print the given pattern
class GFG
{
static void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
System.out.print("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
// base case
if (space == 0)
return;
System.out.print(" ");
System.out.print(" ");
print_space(space - 1);
}
// function to print the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
System.out.println();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
System.out.println();
// recursively calling pattern_lower(
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(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 program to print the given pattern
def print_asterisk(asterisk):
# base case
if (asterisk == 0):
return;
print("* ", end = "");
print_asterisk(asterisk - 1);
def print_space(space):
# base case
if (space == 0):
return;
print(" ", end = "");
print(" ", end = "");
print_space(space - 1);
# function to print the
# upper-half of the pattern
def pattern_upper(n, num):
# base case
if (n == 0):
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
print();
# recursively calling pattern_upper()
pattern_upper(n - 1, num);
# function to print the lower-half
# of the pattern
def pattern_lower(n, num):
if (n == 0):
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
print();
# recursively calling pattern_lower(
pattern_lower(n - 1, num);
# function to print the pattern
def pattern(n, num):
pattern_upper(n, num);
pattern_lower(n - 1, num);
# Driver Code
if __name__ == '__main__':
n = 5;
pattern(n, n);
# This code is contributed by Princi Raj
C#
// C# program to print the given pattern
using System;
class GFG
{
static void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
Console.Write("* ");
print_asterisk(asterisk - 1);
}
static void print_space(int space)
{
// base case
if (space == 0)
return;
Console.Write(" ");
Console.Write(" ");
print_space(space - 1);
}
// function to print
// the upper-half of the pattern
static void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
print_space(2 * n - 1);
print_asterisk(num - n + 1);
Console.WriteLine();
// recursively calling pattern_upper()
pattern_upper(n - 1, num);
}
// function to print the lower-half of the pattern
static void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_space(2 * (num - n) + 1);
print_asterisk(n);
Console.WriteLine();
// recursively calling pattern_lower(
pattern_lower(n - 1, num);
}
// function to print the pattern
static void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n - 1, num);
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
pattern(n, n);
}
}
// This code is contributed by Princi Singh
- 输出:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *