Skip to content

Commit 48948d4

Browse files
Added palindrome number checker in math folder
1 parent b9c118f commit 48948d4

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

math/palindrome_number.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @file
3+
* @brief Implementation to check whether a number is a palindrome.
4+
*
5+
* @details
6+
* A palindrome number is a number that remains the same when its digits are
7+
* reversed.
8+
*
9+
* ### Algorithm
10+
* - Take the input number `n`.
11+
* - Reverse its digits.
12+
* - Compare the reversed number with the original.
13+
* - If both are equal, then the number is a palindrome.
14+
*
15+
* @example
16+
* Input: 121
17+
* Output: Palindrome
18+
*
19+
* Input: 123
20+
* Output: Not Palindrome
21+
*
22+
* \note This implementation uses integer arithmetic and does not convert to
23+
* string.
24+
*
25+
* @author [Ashutosh kumar](https://github.com/Ashutosh-kumar-06)
26+
*/
27+
28+
#include <cassert> /// for assert
29+
#include <iostream> /// for IO operations
30+
31+
/**
32+
* @namespace math
33+
* @brief Mathematical algorithms
34+
*/
35+
namespace math {
36+
/**
37+
* @brief Function to check whether a number is a palindrome.
38+
* @param n input number
39+
* @returns 1 if the number is palindrome, otherwise 0
40+
*/
41+
int isPalindrome_number(int n) {
42+
if (n < 0) { // negative numbers are not palindromes
43+
return 0;
44+
}
45+
46+
int original = n;
47+
int reversed = 0;
48+
49+
while (n > 0) {
50+
int digit = n % 10;
51+
reversed = reversed * 10 + digit;
52+
n /= 10;
53+
}
54+
55+
return (original == reversed) ? 1 : 0;
56+
}
57+
} // namespace math
58+
59+
/**
60+
* @brief Self-test implementations
61+
* @returns void
62+
*/
63+
static void test() {
64+
std::cout << "Test Case 1... ";
65+
assert(math::isPalindrome_number(121) == 1);
66+
std::cout << "Passed!\n";
67+
68+
std::cout << "Test Case 2... ";
69+
assert(math::isPalindrome_number(123) == 0);
70+
std::cout << "Passed!\n";
71+
72+
std::cout << "Test Case 3... ";
73+
assert(math::isPalindrome_number(0) == 1);
74+
std::cout << "Passed!\n";
75+
76+
std::cout << "Test Case 4... ";
77+
assert(math::isPalindrome_number(1221) == 1);
78+
std::cout << "Passed!\n";
79+
80+
std::cout << "\nAll test cases have successfully passed!\n";
81+
}
82+
83+
/**
84+
* @brief Optional user input testing
85+
* @returns void
86+
*/
87+
void user_input_test() {
88+
int n = 0;
89+
std::cout << "Enter a number: ";
90+
std::cin >> n;
91+
92+
if (math::isPalindrome_number(n)) {
93+
std::cout << n << " is a palindrome number.\n";
94+
} else {
95+
std::cout << n << " is not a palindrome number.\n";
96+
}
97+
}
98+
99+
/**
100+
* @brief Main function
101+
* @returns 0 on successful execution
102+
*/
103+
int main() {
104+
test(); // run self-tests
105+
106+
// Uncomment to test manually
107+
// user_input_test();
108+
109+
return 0;
110+
}

0 commit comments

Comments
 (0)