forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindrome.cpp
54 lines (46 loc) · 1.27 KB
/
Palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
// Function to check if a
// given integer is a palindrome
bool palindrome(int n)
{
// Initialize a variable to
// store the reverse of the number
int revNum = 0;
// Create a duplicate variable to
// store the original number
int dup = n;
// Iterate through each digit of
// the number until it becomes 0
while (n > 0) {
// Extract the last
// digit of the number
int ld = n % 10;
// Build the reverse number
// by appending the last digit
revNum = (revNum * 10) + ld;
// Remove the last digit
// from the original number
n = n / 10;
}
// Check if the original number
// is equal to its reverse
if (dup == revNum) {
// If equal, return true
// indicating it's a palindrome
return true;
} else {
// If not equal, return false
// indicating it's not a palindrome
return false;
}
}
int main() {
int number = 4554;
if (palindrome(number)) {
cout << number << " is a palindrome." << endl;
} else {
cout << number << " is not a palindrome." << endl;
}
return 0;
}