-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.java
More file actions
45 lines (36 loc) · 1.04 KB
/
Copy pathPalindrome.java
File metadata and controls
45 lines (36 loc) · 1.04 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
// Java program to check whether a number
// is Palindrome or not.
class Palindrome
{
/* Iterative function to reverse digits of num*/
static int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
/* Function to check if n is Palindrome*/
static int isPalindrome(int n)
{
// get the reverse of n
int rev_n = reverseDigits(n);
// Check if rev_n and n are same or not.
if (rev_n == n)
return 1;
else
return 0;
}
/*Driver program to test reverseDigits*/
public static void main(String []args)
{
int n = 4562;
System.out.println("Is" + n + "a Palindrome number? -> " +
(isPalindrome(n) == 1 ? "true" : "false"));
n = 2002;
System.out.println("Is" + n + "a Palindrome number? -> " +
(isPalindrome(n) == 1 ? "true" : "false"));
}
}