forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome_number.cpp
62 lines (52 loc) · 1.18 KB
/
Palindrome_number.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
/*
Description :
Palindrome Number : Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
This code is implemented without using extra space.
*/
#include <bits/stdc++.h>
using namespace std;
bool checkPalindrome(int num)
{
// divi = divisor
int divi = 1;
//finding the apt. divisor for the number
while (num / divi >= 10)
{
divi *= 10;
}
while (num != 0)
{
int leading = num / divi;
int trailing = num % 10;
if (leading != trailing)
{
return false;
}
//reducing number to compare
num = (num % divi) / 10;
divi = divi / 100;
}
return true;
}
int main()
{
int num;
cout << "Enter the number to check : " << endl;
cin >> num;
checkPalindrome(n) ? cout << "True" : cout << "False";
return 0;
}
/*
Time complexity : O(n2)
Space complexity : O(1)
*/
/*
Test Case :
Test case 1 :
Input : 1001
Output : True
Test case 2 :
Input : 1234
Output : False
*/