forked from Ishaan28malik/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.java
44 lines (40 loc) · 781 Bytes
/
pattern.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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