-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q_10.java
91 lines (76 loc) · 2.26 KB
/
Q_10.java
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
81
82
83
84
85
86
87
88
89
90
91
// Muhammad Naveed
// (Palindromes) A palindrome is a sequence of characters that reads the same
// backward as forward. For example, each of the following five-digit integers
// is a palindrome: 12321, 55555, 45554 and 11611. Write an application that reads
// in a five-digit integer and determines whether it’s a palindrome.
// If the number is not five digits long, display an error message and allow the user
// to enter a new value.
import java.util.Scanner;
public class Q_10
{
private int number = 0;
private final Scanner sc = new Scanner(System.in);
// getting user input method
public void readNumber()
{
System.out.print("Enter the number : ");
number = sc.nextInt();
//sc.close();
validateNumber();
sc.close();
}
// validating user input method
private void validateNumber()
{
// if ((number >= 10000) && (number <= 99999))
// {
// System.out.println(isPalindrome());
// }
// else
// {
// System.out.println("Invalid input! \nPlease try again");
// readNumber();
// }
String str = Integer.toString(number);
if (str.length() == 5)
{
System.out.println(isPalindrome());
}
else
{
System.out.println("Invalid input! \nPlease try again");
readNumber();
}
}
// Palindrome number finder method
private String isPalindrome()
{
int originalNumber = number;
int reverse = 0;
int rem ;
// reversing the integer number like 123 => 321
while(number != 0)
{
rem = number % 10;
reverse = reverse * 10 + rem;
number /= 10;
}
// returning the string you may also change the return type to void
// and print these strings rather than returning.
if (reverse == originalNumber)
{
return "The given number is palindrome number";
}
else
{
return "The given number is not palindrome number";
}
}
// main driven method.
public static void main(String[] args)
{
// creating the object.
Q_10 obj = new Q_10();
obj.readNumber();
}
}