-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
54 lines (53 loc) · 1.67 KB
/
Main.java
File metadata and controls
54 lines (53 loc) · 1.67 KB
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
45
46
47
48
49
50
51
52
53
54
package creatingMethodsByPattern;
import java.util.Scanner;
/**
* @author Fatih ARI - 21.08.2021
*
* According to the n value received from the user, the program is designed using a "Recursive" method without using a loop that follows the rule below.
* Rules:
* Subtract 5 from the entered number up to where the entered number is 0 or negative.
* During each subtraction, the last value is printed on the screen.
* After the number becomes negative or 0, 5 is added again.
* Again, in each addition operation, the last value of the number is printed on the screen.
*
*/
public class Main {
private static boolean sequence(int n, int temp)
{
if(temp<=0)
{
int temp2=n;
n=temp;
temp = temp2;
}
if(temp<=n){
System.out.print(temp +" ");
return sequence(n, temp-5);
}
if(n+5==temp){
System.out.print(n +" " + (n+5) +" ");
return true;
}
if(n<temp)
{
System.out.print(n +" ");
return sequence(n+5, temp);
}
return false;
}
public static void main(String[] args) {
clearScreen();
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
int temp = n;
input.close();
System.out.print("Output: ");
sequence(n, temp);
}
//It is used for console screen cleaning in Java.
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}