Skip to content

Commit 09d2cd8

Browse files
committed
feat(math): add palindrome number checker in C++ without non-standard headers
1 parent b9c118f commit 09d2cd8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

math/palindrome_number.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
5+
// Function to check if a number is a palindrome
6+
bool is_palindrome_number(long long n) {
7+
if (n < 0)
8+
return false; // Negative numbers are not palindromes
9+
std::string s = std::to_string(n);
10+
int i = 0, j = static_cast<int>(s.size()) - 1;
11+
while (i < j) {
12+
if (s[i] != s[j])
13+
return false;
14+
++i;
15+
--j;
16+
}
17+
return true;
18+
}
19+
20+
#ifdef RUN_LOCAL
21+
int main() {
22+
std::vector<long long> tests = {121, -121, 10, 12321, 0};
23+
for (auto t : tests) {
24+
std::cout << t << " -> " << (is_palindrome_number(t) ? "True" : "False")
25+
<< "\n";
26+
}
27+
return 0;
28+
}
29+
#endif

0 commit comments

Comments
 (0)