-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeCheck.java
More file actions
81 lines (56 loc) · 1.57 KB
/
PalindromeCheck.java
File metadata and controls
81 lines (56 loc) · 1.57 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Scanner;
import static java.lang.Math.abs;
import static java.lang.Math.pow;
public class Palindrome {
private static int input = 0;
public static void main( String[] args )
{
Scanner in = new Scanner(System.in);
try {
System.out.println("Please Enter the integer you want to check if palindrome");
input = in.nextInt();
}catch (java.util.InputMismatchException e){
System.out.println( "Please only Enter Integers");
System.exit(0);
}
System.out.println(palindrome(input));
}
private static boolean palindrome(int x){
int num1 = abs(x);
int num2=num1;
int count=0;
int n =0;
int y = 0;
int digit = 0;
for(; num1 >0; num1/=10, ++digit) {
}
int digit2=digit;
int[] digits=new int[digit];
for(; digit >0; --digit,++n) {
for (; num2 >= Math.pow(10,digit-1);num2-= Math.pow(10,digit-1), ++count) {
}
digits[n]=count;
count = 0;
}
for(;digit2>y;y++){
digits[y]*=Math.pow(10,y);
}
double reversed = 0;
for (int value : digits) {
reversed += value;
}
if(x<0){
reversed= reversed*-1;
}
if (reversed>2147483647 ||reversed<-2147483647){
return false;
}
int g = (int)reversed;
if (g>=0&&g==x){
return true;
}
else{
return false;
}
}
}