-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
36 lines (27 loc) · 972 Bytes
/
Main.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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter N value
System.out.print("Enter N value: ");
int n = scanner.nextInt();
// Call the recursive method with the entered N value
pattern(n, n);
scanner.close();
}
// Recursive method to follow the specified rule
public static void pattern(int originalN, int currentN) {
// Print the current value
System.out.print(currentN + " ");
// Base case: Terminate the process when the specified conditions are met
if (currentN <= 0) {
return;
}
// Recursive case: Subtract 5 successively
pattern(originalN, currentN - 5);
// Start adding 5 when the number becomes negative or 0
if (currentN <= 0) {
pattern(originalN, currentN + 5);
}
}
}