forked from 1ramagrawal0610/h4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathram3.java
More file actions
127 lines (100 loc) · 2.74 KB
/
ram3.java
File metadata and controls
127 lines (100 loc) · 2.74 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class Main {
public static void main(String[] args) {
String str = "Radar", reverseStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
}
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
Run Code
Output
Radar is a Palindrome String.
In the above example, we have a string "Radar" stored in str. Here, we have used the
1. for loop to reverse the string
The loop runs from the end to the beginning of the string.
The charAt() method accesses each character of the string.
Each character of the string is accessed in reverse order and stored in reverseStr.
2. if statement to compare str and reverseStr
The toLowerCase() method converts both str and reverseStr to lowercase. This is because Java is case sensitive and 'r' and 'R' are two different values.
The equals() method checks if two strings are equal.
Example 2: Java Program to Check Palindrome Number
class Main {
public static void main(String[] args) {
int num = 3553, reversedNum = 0, remainder;
// store the number to originalNum
int originalNum = num;
// get the reverse of originalNum
// store it in variable
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
// check if reversedNum and originalNum are equal
if (originalNum == reversedNum) {
System.out.println(originalNum + " is Palindrome.");
}
else {
System.out.println(originalNum + " is not Palindrome.");
}
}
}
Run Code
Output
3553 is Palindrome.
In the above example, we have a number 3553 stored in num and originalNum variables. Here, we have used the
while loop to reverse num and store the reversed number in reversedNum
if...else to check if reversedNum is same as the originalNum
Share on:
Did you find this article helpful?
Related Examples
Java Example
Reverse a Number
Java Example
Reverse a Sentence Using Recursion
Java Example
Check Armstrong Number
Java Example
Differentiate String == operator and equals() method
Join our newsletter for the latest updates.
Enter Email Address*
Join
Tutorials
Python 3 Tutorial
JavaScript Tutorial
SQL Tutorial
C Tutorial
Java Tutorial
Kotlin Tutorial
C++ Tutorial
Swift Tutorial
C# Tutorial
Go Tutorial
DSA Tutorial
Examples
Python Examples
JavaScript Examples
C Examples
Java Examples
Kotlin Examples
C++ Examples
Company
About
Advertising
Privacy Policy
Terms & Conditions
Contact
Blog
Youtube
Apps
Learn Python
Learn C Programming
Learn Java
© Parewa Labs Pvt. Ltd. All rights reserved.