Ques:-21) – Write a program to check whether number entered through keyboard is palindrome or not.

Here’s a C program that checks whether a number entered through the keyboard is a palindrome or not:

				
					#include"stdio.h"
#include"conio.h"
void main()
{
  int num,revnum=0,num1,rem=0;
  clrscr();
  printf("enter any number:");
  scanf("%d",&num);
  num1=num;
  for(num;num>0;num=num/10)
  {
      rem=num%10;
      revnum=(revnum*10)+rem;
  }
  if(revnum==num1)
    printf("\nnumber is palindrome");
  else
    printf("\nnumber is not palindrome");
  getch();

				
			

Out Put

				
					Enter any number: 12
number is not palindrome
				
			

Another Example

				
					#include <stdio.h>

int main() {
    int num, reversedNum = 0, remainder, originalNum;

    // Prompt user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Store the original number
    originalNum = num;

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }

    // Check if the original number is equal to its reverse
    if (originalNum == reversedNum) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }

    return 0;
}

				
			

Explanation

  1. Variables:

    • num: to store the number entered by the user.
    • reversedNum: to store the reversed number.
    • remainder: to store the remainder when calculating the reverse.
    • originalNum: to store the original number for comparison.
  2. Input:

    • The program prompts the user to enter an integer.
    • The entered number is read using scanf and stored in num.
  3. Reverse Calculation:

    • The while loop runs until num becomes 0.
    • In each iteration, the last digit of num is obtained using num % 10 and stored in remainder.
    • reversedNum is updated by multiplying it by 10 and adding the remainder.
    • num is divided by 10 to remove the last digit.
  4. Palindrome Check:

    • After reversing, the program compares originalNum with reversedNum.
    • If they are equal, the number is a palindrome. Otherwise, it is not.
  5. Output:

    • The result is printed based on the comparison.

Sample Output

Example 1: Palindrome Number

				
					Enter an integer: 121
121 is a palindrome.

				
			

Example 2: Not a Palindrome Number

				
					Enter an integer: 123
123 is not a palindrome.

				
			

This C program effectively checks if the entered number is a palindrome by reversing the digits and comparing the reversed number to the original.

Scroll to Top