Example: #1 - Check the number is a Palindrome
Check the number is a Palindrome using a divide and modulus approach
C++ Input Screen
#include <iostream>
using namespace std;
class PalindromeExample
{
// Access specifier
public:
void checkPalindrome(int num) {
int originalNumber = num;
int remainder, reverseNumber = 0;
int temp = originalNumber;
while (temp > 0) {
remainder = temp % 10;
reverseNumber = (reverseNumber * 10) + remainder;
temp = temp / 10;
}
if (originalNumber == reverseNumber) {
cout << "The given number is Palindrome" << endl;
} else {
cout << "The given number is NOT a Palindrome" << endl;
}
}
};
int main(int argc, const char * argv[])
{
// Declare an object of class geeks
PalindromeExample obj;
obj.checkPalindrome(12321);
return 0;
}
C++ Output Screen
The given number is Palindrome