C program to check whether a number is palindrome or not

Write a C program to enter any number and check whether given number is palindrome or not using for loop. How to check whether a number is palindrome or not in C programming. C program for palindrome numbers.

Input number: 121
Output: 121 is Palindrome

Required knowledge

Basic C programming, If else, For loop

Palindrome numbers

Palindrome numbers are such number which when reversed are equal to the original number.
For example: 121
Reverse of 121 is also equal to 121. Hence 121 is a palindrome number.

Logic to check palindrome numbers

From the definition of the palindrome numbers it must be clear to all that in-order to check whether a number is palindrome or not we must find the reverse of the number. After retrieving the reverse of number we just need to make a conditional check that whether the given number is equal to its reverse or not. If the given number and its reverse are same then the number is palindrome otherwise not.

Program to check palindrome number

 * C program to check whether a number is palindrome or not

#include <stdio.h>

int main()
    int n, num, rev = 0;

    /* Reads a number from user */
    printf("Enter any number to check palindrome: ");
    scanf("%d", &n);

    num = n; //Copies original value to num.

    /* Finds reverse of n and stores in rev */
        rev = (rev * 10) + (n % 10);
        n = n/10;

    /* Check if reverse is equal to original num or not */
        printf("%d is palindrome.", num);
        printf("%d is not palindrome.", num);

    return 0;

Note: After learning this also advance your skills by learning this using recursive function. Learn how to check palindrome numbers using recursion.

Enter any number to check palindrome: 121
121 is palindrome.

Happy coding ;)

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment