From 48948d402298c651492468dbb56b976c81e7d517 Mon Sep 17 00:00:00 2001 From: Ashutosh-kumar-06 Date: Tue, 21 Oct 2025 00:50:27 +0530 Subject: [PATCH] Added palindrome number checker in math folder --- math/palindrome_number.cpp | 110 +++++++++++++++++++++++++++++++++++++ 1 file changed, 110 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..a7e328cfff --- /dev/null +++ b/math/palindrome_number.cpp @@ -0,0 +1,110 @@ +/** + * @file + * @brief Implementation to check whether a number is a palindrome. + * + * @details + * A palindrome number is a number that remains the same when its digits are + * reversed. + * + * ### Algorithm + * - Take the input number `n`. + * - Reverse its digits. + * - Compare the reversed number with the original. + * - If both are equal, then the number is a palindrome. + * + * @example + * Input: 121 + * Output: Palindrome + * + * Input: 123 + * Output: Not Palindrome + * + * \note This implementation uses integer arithmetic and does not convert to + * string. + * + * @author [Ashutosh kumar](https://github.com/Ashutosh-kumar-06) + */ + +#include /// for assert +#include /// for IO operations + +/** + * @namespace math + * @brief Mathematical algorithms + */ +namespace math { +/** + * @brief Function to check whether a number is a palindrome. + * @param n input number + * @returns 1 if the number is palindrome, otherwise 0 + */ +int isPalindrome_number(int n) { + if (n < 0) { // negative numbers are not palindromes + return 0; + } + + int original = n; + int reversed = 0; + + while (n > 0) { + int digit = n % 10; + reversed = reversed * 10 + digit; + n /= 10; + } + + return (original == reversed) ? 1 : 0; +} +} // namespace math + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + std::cout << "Test Case 1... "; + assert(math::isPalindrome_number(121) == 1); + std::cout << "Passed!\n"; + + std::cout << "Test Case 2... "; + assert(math::isPalindrome_number(123) == 0); + std::cout << "Passed!\n"; + + std::cout << "Test Case 3... "; + assert(math::isPalindrome_number(0) == 1); + std::cout << "Passed!\n"; + + std::cout << "Test Case 4... "; + assert(math::isPalindrome_number(1221) == 1); + std::cout << "Passed!\n"; + + std::cout << "\nAll test cases have successfully passed!\n"; +} + +/** + * @brief Optional user input testing + * @returns void + */ +void user_input_test() { + int n = 0; + std::cout << "Enter a number: "; + std::cin >> n; + + if (math::isPalindrome_number(n)) { + std::cout << n << " is a palindrome number.\n"; + } else { + std::cout << n << " is not a palindrome number.\n"; + } +} + +/** + * @brief Main function + * @returns 0 on successful execution + */ +int main() { + test(); // run self-tests + + // Uncomment to test manually + // user_input_test(); + + return 0; +}