-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathpalindromeleetcode.cpp
65 lines (50 loc) · 1.3 KB
/
palindromeleetcode.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
55
56
57
58
59
60
61
62
63
64
65
//to check whether a number is a palindrome number or not
class Solution {
public:
int reverse(int x) {
long long int t = x;
long long int rev = 0;
while(t != 0){
// While t is not equal to 0,
int a = t % 10; // store last digit of the t
rev = (rev * 10) + a; // and add a to the rev
t = t/10;
}
// Finally we get reverse number
return rev;
}
bool isPalindrome(int x) {
// If negative number return false;
if(x < 0){
return false;
}
int t = reverse(x);
// reverse it and compare with x if same return true else false
if(t == x){
return true;
}
else{
return false;
}
}
};
Program to check whether a number is palindrom is using temp variable.
#include <iostream>
using namespace std;
int main ()
{
int num, reverse = 0, rem, temp;
num=12321;
cout <<"\nThe number is: "<<num;
temp = num;
while(temp != 0)
{
rem = temp % 10;
reverse = reverse * 10 + rem;
temp /= 10;
};
if (num == reverse)
cout << num << " is Palindrome";
else
cout << num << " is not a Palindrome";
}