-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome checking by converting number to string.cpp
70 lines (61 loc) · 1.33 KB
/
palindrome checking by converting number to string.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
66
67
68
69
70
// Program to check if a number is palindrome or not using header file and by converting number to string
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n;
cout << "Enter a number:- " << endl;
cin >> n;
string str = to_string(n);
bool isPalindrome = true;
int j = strlen(str.c_str()) - 1;
for(int i=0; i<j; i++, j--)
{
if(str[i] != str[j])
{
isPalindrome = false;
break;
}
}
if(isPalindrome)
{
cout << n << " is a palindrome number" << endl;
}
else
{
cout << n << " is not a palindrome number" << endl;
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Program to check whether a number is palindrome or not using normal method of reversing a number and comparing with original number
#include<iostream>
using namespace std;
int getReverseNumber(int n)
{
int reverse = 0;
int right_most_digit;
while(n != 0)
{
right_most_digit = n%10;
reverse = (reverse*10) + right_most_digit;
n = n/10;
}
return reverse;
}
int main()
{
int n;
cout << "Enter a number:- " << endl;
cin >> n;
if(n == getReverseNumber(n))
{
cout << n << " is a palindrome number" << endl;
}
else
{
cout << n << " is not a palindrome number" << endl;
}
return 0;
}