Example output
// This C++ program will determine if a number is a palindrome.
// A palindrome is a number or word that reads the same backwards and forwards.
#include <iostream>
#include <string>
// Function to determine if a number is a palindrome
bool isPalindrome(int num)
{
// Convert int to string
std::string str = std::to_string(num);
// Compare characters from the beginning and end of the string
for (int i = 0; i < str.length()/2; i++)
{
if (str[i] != str[str.length() - i - 1])
{
// If the characters don't match, return false
return false;
}
}
// If all characters match, return true
return true;
}
int main()
{
// Test the function
int num = 1221;
if (isPalindrome(num))
{
std::cout << num << " is a palindrome" << std::endl;
}
else
{
std::cout << num << " is not a palindrome" << std::endl;
}
return 0;
}
/*
Explanation:
This code first includes the iostream and string libraries. Then, the isPalindrome() function is declared, which takes an integer as a parameter and returns true if it is a palindrome and false if it is not. The function first converts the integer to a string, then loops through the characters at each end of the string and compares them. If they are not equal, the function returns false. If they are equal, the function returns true. Finally, the main() function is declared, which tests the isPalindrome() function by passing in the integer 1221. The program prints out whether or not the number is a palindrome.
*/