-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_9.cpp
41 lines (34 loc) · 1.37 KB
/
Leetcode_9.cpp
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
// Copy only the CLASS to submit on Leetcode !!
#include<iostream> // Include input-output library
using namespace std;
// Define a class named Solution
class Solution {
public:
// Function to check if a given integer is a palindrome
bool isPalindrome(int x) {
int m = x; // Store the original value of x to compare later
double revNum = 0; // Variable to store the reversed number
// Reverse the number
while(x > 0) { // Continue until all digits are processed
int lastDigit = x % 10; // Extract the last digit
revNum = (revNum * 10) + lastDigit; // Build the reversed number
x = x / 10; // Remove the last digit from x
}
// Check if the reversed number is equal to the original number
if(revNum == m)
return true; // Return true if the number is a palindrome
else
return false; // Return false otherwise
}
};
// Main function to execute the program
int main() {
Solution s; // Create an object of the Solution class
// Check if 121 is a palindrome and print the result
if(s.isPalindrome(121)) {
cout << "The given number is Palindrome"; // Output if it is a palindrome
} else {
cout << "The given number is not a Palindrome"; // Output if it is not a palindrome
}
return 0; // Return 0 to indicate successful execution
}