-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
42 lines (42 loc) · 1.25 KB
/
Copy pathMain.java
File metadata and controls
42 lines (42 loc) · 1.25 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
package primeNumberWithRecursive;
import java.util.Scanner;
/**
* @author Fatih ARI - 21.08.2021
*
* The program is designed to check whether the number entered by the user is a prime number by using recursive methods.
*
*/
public class Main
{
public static boolean isPrime(int x, int y)
{
if(y==1)
return true;
if(x%y==0)
return false;
return isPrime(x, y-1);
}
public static void main(String[] args)
{
clearScreen();
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int x = input.nextInt();
input.close();
if(x>1)
{
int y = x/2; //x is divisible by its middle value at most.
if(isPrime(x,y))
System.out.println("YES! " + x + " is prime a number.\n");
else
System.out.println("NO! " + x + " is NOT a prime number.\n");
}
else
System.out.println("Please enter a number greater than 1!\n");
}
//It is used for console screen cleaning in Java.
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}