mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
36 lines
780 B
C++
36 lines
780 B
C++
/**
|
|
* @file
|
|
* @brief Check if a number is
|
|
* [palindrome](https://en.wikipedia.org/wiki/Palindrome) or not.
|
|
*
|
|
* This program cheats by using the STL library's std::reverse function.
|
|
*/
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
|
|
#ifdef _MSC_VER
|
|
// Required to compile std::toString function using MSVC
|
|
#include <string>
|
|
#else
|
|
#include <cstring>
|
|
#endif
|
|
|
|
/** Main function */
|
|
int main() {
|
|
int num;
|
|
std::cout << "Enter number = ";
|
|
std::cin >> num;
|
|
|
|
std::string s1 = std::to_string(num); // convert number to string
|
|
std::string s2 = s1;
|
|
|
|
std::reverse(s1.begin(), s1.end()); // reverse the string
|
|
|
|
if (s1 == s2) // check if reverse and original string are identical
|
|
std::cout << "true";
|
|
else
|
|
std::cout << "false";
|
|
|
|
return 0;
|
|
}
|