forked from waboke/solutions_to_pastquestions_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromecheck.cpp
More file actions
30 lines (24 loc) · 773 Bytes
/
Copy pathpalindromecheck.cpp
File metadata and controls
30 lines (24 loc) · 773 Bytes
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
#include <iostream>
using namespace std;
// Function to check if a number is a palindrome
bool isPalindrome(int num) {
int original = num; // Store the original number
int reversed = 0, digit;
while (num > 0) {
digit = num % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Build the reversed number
num /= 10; // Remove the last digit
}
return original == reversed; // Compare the original and reversed numbers
}
int main() {
int number;
cout << "Enter a number to check if it's a palindrome: ";
cin >> number;
if (isPalindrome(number)) {
cout << number << " is a palindrome." << endl;
} else {
cout << number << " is not a palindrome." << endl;
}
return 0;
}