Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions math/palindrome_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file
* @brief Check if a given number is a palindrome
* @details
* This program defines a function `is_palindrome_number()` which checks whether
* a number reads the same backward as forward.
* Negative numbers are treated as non-palindromic.
*
* @see https://en.wikipedia.org/wiki/Palindrome
*/

#include <iostream>
#include <string>
#include <vector>

/**
* @brief Checks if an integer is a palindrome number.
* @param n The number to check.
* @return `true` if `n` is a palindrome, otherwise `false`.
*/
bool is_palindrome_number(long long n) {
if (n < 0)
return false;
std::string s = std::to_string(n);
int i = 0, j = static_cast<int>(s.size()) - 1;
while (i < j) {
if (s[i] != s[j])
return false;
++i;
--j;
}
return true;
}

#ifdef RUN_LOCAL
/**
* @brief Example test cases for the palindrome number checker.
*/
int main() {
std::vector<long long> tests = {121, -121, 10, 12321, 0};
for (auto t : tests) {
std::cout << t << " -> " << (is_palindrome_number(t) ? "True" : "False")
<< "\n";
}
return 0;
}
#endif