C++ Program to Check if Number is Palindrome or Not using For Loop

C++ Program to Check if Number is Palindrome or Not using For Loop

Write C++ Program to Check if Number is Palindrome or Not using For Loop

// CPP Program to Check if Number is Palindrome or Not using For Loop

#include <iostream>

using namespace std;

int main()
{
	long int n, ans = 0;

	cout << "Enter the number:--> ";
	cin >> n;  //User input:-422

	long int temp = n;

	for(; n != 0; )
	{
		int remainder = n % 10;  //2  //2  //4
		ans = ans * 10 + remainder;  //2  //22  //224
		n = n / 10;   //42  //4
	}
	if(temp == ans)
	{
		cout << "The given number is palindrome";
	}
	else
	{
		cout << "The given number is not palindrome";
	}

	return 0;
}

Output:

Run 1:
Enter the number:--> 456
The given number is not palindrome

Run 2:
Enter the number:--> 121
The given number is palindrome