From 09d2cd89964e71828f9a57a8e7ca20bb101f90ed Mon Sep 17 00:00:00 2001 From: Siddharth Sandilaya Date: Thu, 23 Oct 2025 15:13:13 +0530 Subject: [PATCH 1/2] feat(math): add palindrome number checker in C++ without non-standard headers --- math/palindrome_number.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 math/palindrome_number.cpp diff --git a/math/palindrome_number.cpp b/math/palindrome_number.cpp new file mode 100644 index 0000000000..12df2489b3 --- /dev/null +++ b/math/palindrome_number.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +// Function to check if a number is a palindrome +bool is_palindrome_number(long long n) { + if (n < 0) + return false; // Negative numbers are not palindromes + std::string s = std::to_string(n); + int i = 0, j = static_cast(s.size()) - 1; + while (i < j) { + if (s[i] != s[j]) + return false; + ++i; + --j; + } + return true; +} + +#ifdef RUN_LOCAL +int main() { + std::vector tests = {121, -121, 10, 12321, 0}; + for (auto t : tests) { + std::cout << t << " -> " << (is_palindrome_number(t) ? "True" : "False") + << "\n"; + } + return 0; +} +#endif \ No newline at end of file From 04985be498065b9e743d8b92559dbf3fbbbe1563 Mon Sep 17 00:00:00 2001 From: Siddharth Sandilaya Date: Thu, 23 Oct 2025 15:18:33 +0530 Subject: [PATCH 2/2] feat(math): add palindrome number checker using standard headers --- math/palindrome_number.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/math/palindrome_number.cpp b/math/palindrome_number.cpp index 12df2489b3..46f383d166 100644 --- a/math/palindrome_number.cpp +++ b/math/palindrome_number.cpp @@ -1,11 +1,26 @@ +/** + * @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 #include #include -// Function to check if a number is a palindrome +/** + * @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; // Negative numbers are not palindromes + return false; std::string s = std::to_string(n); int i = 0, j = static_cast(s.size()) - 1; while (i < j) { @@ -18,6 +33,9 @@ bool is_palindrome_number(long long n) { } #ifdef RUN_LOCAL +/** + * @brief Example test cases for the palindrome number checker. + */ int main() { std::vector tests = {121, -121, 10, 12321, 0}; for (auto t : tests) {