-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathpoorvimishra_CSE5.c
More file actions
36 lines (29 loc) · 840 Bytes
/
Copy pathpoorvimishra_CSE5.c
File metadata and controls
36 lines (29 loc) · 840 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
32
33
34
35
36
// Question statement: Given an integer x, return true if x is a palindrome, and false otherwise.
#include<iostream>
using namespace std;
bool isPalindrome(int x){
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int original = x;
int reversed = 0;
// Reverse the entire integer
while (x > 0){
int digit = x % 10;
reversed = reversed * 10 + digit;
x /= 10;
}
// Compare the reversed number with the original
return original == reversed;
}
int main() {
int x = 555;
cout << (isPalindrome(x) ? "true" : "false") << endl;
x = 343;
cout << (isPalindrome(x) ? "true" : "false") << endl;
x = 687;
cout << (isPalindrome(x) ? "true" : "false") << endl;
x = 13;
cout << (isPalindrome(x) ? "true" : "false") << endl;
return 0;
}