Open In App

# Programs to print Triangle and Diamond patterns using recursion

1. Pattern 1: Example:
Input: 5
Output:
* * * * *   * * * * *
* * * *       * * * *
* * *           * * *
* *               * *
*                   *
1. Implementation:

## 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 patternvoid 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 functionint main(){    int n = 5;    pattern(n, n);    return 0;}

## Java

 // Program to print the given patternimport 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 patternstatic 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 Codepublic static void main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by PrinciRaj1992

## Python3

 # Program to print the given patterndef 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 patterndef 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 Codeif __name__ == '__main__':    n = 5;    pattern(n, n); # This code is contributed by Princi Singh

## C#

 // C# Program to print the given patternusing 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 patternstatic 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 Codepublic static void Main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by PrinciRaj1992

## JavaScript



1. Output:
* * * * *   * * * * *
* * * *       * * * *
* * *           * * *
* *               * *
*                   *

Time Complexity: O(n),The time complexity of the above code is O(n) as the function is called n times.
Space Complexity: O(n),The space complexity of the above code is O(n) as the recursive call stack is of size n.

1. Pattern 2: Example:
Input: 5
Output:
*                   *
* *               * *
* * *           * * *
* * * *       * * * *
* * * * *   * * * * *
1. Implementation:

## 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 patternvoid 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 functionint main(){    int n = 5;    pattern(n, n);    return 0;}

## Java

 // Java  program to print the given patternclass 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 patternstatic 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 Codepublic static void main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by Princi Singh

## Python3

 # Program to print the given patterndef 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 patterndef 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 Codeif __name__ == '__main__':    n = 5;    pattern(n, n);     # This code is contributed by Princi Singh

## C#

 // C# program to print the given patternusing 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 patternstatic 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 Codepublic static void Main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by Rajput-Ji

## JavaScript



1. Output:
*                   *
* *               * *
* * *           * * *
* * * *       * * * *
* * * * *   * * * * *

Time complexity: O(n),The time complexity of the above program is O(n) where n is the number of asterisks in the pattern.

Space complexity: O(n),The space complexity of the above program is O(n) because of the recursive stack that is used for the recursive calls.

1. Pattern 3: Example:
Input: 5
Output:
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
1. Implementation:

## 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 patternvoid 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 patternvoid 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 patternvoid pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // driver functionint main(){    int n = 5;    pattern(n, n);    return 0;}

## Java

 // Program to print the given patternclass 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 patternstatic 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 patternstatic 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 patternstatic void pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // Driver Codepublic static void main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by 29AjayKumar

## Python3

 # Python3 program to print the given patterndef print_asterisk(asterisk):    if (asterisk == 0):        return;    print("*", end = " ");    print_asterisk(asterisk - 1); # function to print the# upper-half of the patterndef 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 patterndef 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 patterndef pattern(n, num):    pattern_upper(n, num);    pattern_lower(n - 1, num); # Driver Codeif __name__ == '__main__':    n = 5;    pattern(n, n); # This code is contributed by Rajput-Ji

## C#

 // Program to print the given patternusing 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 patternstatic 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 patternstatic 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 patternstatic void pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // Driver Codepublic static void Main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by 29AjayKumar

## JavaScript



1. Output:
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *

Time complexity: O(n^2), where n is the input size.

Space complexity: O(n), where n is the input size.

1. Pattern 4: Example:
Input: 5
Output:
* * * * *   * * * * *
* * * *       * * * *
* * *           * * *
* *               * *
*                   *
* *               * *
* * *           * * *
* * * *       * * * *
* * * * *   * * * * *
1. Implementation:

## 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 patternvoid 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 patternvoid 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 patternvoid pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // driver functionint main(){    int n = 5;    pattern(n, n);    return 0;}

## Java

 // Java Program to print the given patternimport 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 patternstatic 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 patternstatic 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 patternstatic void pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // driver functionpublic static void main(String[] args){    int n = 5;    pattern(n, n);   }} // This code is contributed by 29AjayKumar

## Python3

 # Python3 program to print the given patterndef 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 patterndef 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 patterndef 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 patterndef pattern(n, num):     pattern_upper(n, num);    pattern_lower(n - 1, num); # Driver Codeif __name__ == '__main__':    n = 5;    pattern(n, n); # This code is contributed by Rajput Ji

## C#

 // C# Program to print the given patternusing 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 patternstatic 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 patternstatic 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 patternstatic void pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // Driver Codepublic static void Main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by 29AjayKumar

## JavaScript



1. Output:
* * * * *   * * * * *
* * * *       * * * *
* * *           * * *
* *               * *
*                   *
* *               * *
* * *           * * *
* * * *       * * * *
* * * * *   * * * * *
1. Pattern 5: Example:
Input: 5
Output:
*                   *
* *               * *
* * *           * * *
* * * *       * * * *
* * * * *   * * * * *
* * * *       * * * *
* * *           * * *
* *               * *
*                   *
1. Implementation:

## 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 patternvoid 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 patternvoid 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 patternvoid pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // driver functionint main(){    int n = 5;    pattern(n, n);    return 0;}

## Java

 // Java program to print the given patternclass 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 patternstatic 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 patternstatic 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 patternstatic void pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // Driver Codepublic 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 patterndef 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 patterndef 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 patterndef 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 patterndef pattern(n, num):     pattern_upper(n, num);    pattern_lower(n - 1, num); # Driver Codeif __name__ == '__main__':    n = 5;    pattern(n, n); # This code is contributed by Princi Raj

## C#

 // C# program to print the given patternusing 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 patternstatic 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 patternstatic 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 patternstatic void pattern(int n, int num){    pattern_upper(n, num);    pattern_lower(n - 1, num);} // Driver Codepublic static void Main(String[] args){    int n = 5;    pattern(n, n);}} // This code is contributed by Princi Singh

## JavaScript



1. Output:
*                   *
* *               * *
* * *           * * *
* * * *       * * * *
* * * * *   * * * * *
* * * *       * * * *
* * *           * * *
* *               * *
*                   *