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

Write a 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

#include<stdio.h>

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

	printf("Enter the number:--> ");
	scanf("%ld", &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)
	{
		printf("The given number is palindrome");
	}
	else
	{
		printf("The given number is not palindrome");
	}

	return 0;
}

Output:

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

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

≪ Previous | Next ≫