-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevnum.java
More file actions
31 lines (29 loc) · 775 Bytes
/
Copy pathrevnum.java
File metadata and controls
31 lines (29 loc) · 775 Bytes
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
public class revnum {
public static void main(String[] args) {
int sum = 0;
int n = 12321;
int ans = rev(n,sum);
System.out.println(ans);
ans=rev2(n);
System.out.println(ans);
System.out.println(palindrome(n));
}
static int rev(int n,int sum){
sum = sum*10 + (n%10);
if(n%10==n)
return sum;
return rev(n/10,sum);
}
static int rev2(int n){
int digits = (int)(Math.log10(n))+1;
return helper(n,digits);
}
static int helper(int n,int digits){
if(n%10==n)
return n;
return (n%10)*(int)Math.pow(10,digits-1) + helper(n/10,--digits);
}
static boolean palindrome(int n){
return (n==rev2(n));
}
}