C Program To Check Whether a Number is Palindrome or Not

In this post, we will check how to check whether a number is palindrome or not using C Programming language.

C Program To Check Whether a Number is Palindrome or Not

A number is a palindrome if even after reversing the digits the number remains the same.

For example: 5225 after reversing the digits (5225) remains the same,  hence it is a palindrome number.

So, without further ado, let’s begin the tutorial.

C Program To Check Whether a Number is Palindrome or Not

// C Program To Check Whether a Number is Palindrome or Not
#include <stdio.h>
int main(){
    int num, reverse = 0, remainder, temp = 0;
    
    // Asking for Input
    printf("Enter an Integer: ");
    scanf("%d", &num);
    
    // reversed integer is stored in temp
    temp = num;
    while (temp != 0){
        remainder = temp % 10;
        reverse = reverse * 10 + remainder;
        temp = temp / 10;
    }
    if (reverse == num){
        printf("%d is a Palindrome.", num);
    }
    else{
        printf("%d is not a Palindrome.", num);
        return 0;
    }
}

Output 1

Enter an Integer: 1221
1221 is a Palindrome.

Output 2

Enter an Integer: 1831
1831 is not a Palindrome.

How Does This Program Work ?

    int num, reverse = 0, remainder, temp = 0;

In this example, we have declared four int data type variables named as num, reverse, remainder and temp. The values of reverse and temp variables are assigned 0.

    // Asking for Input
    printf("Enter an Integer: ");
    scanf("%d", &num);

Then, the user is asked to enter the value of the integer. This value will get stored in the num named variable.

    // reversed integer is stored in temp
    temp = num;
    while (temp != 0){
        remainder = temp % 10;
        reverse = reverse * 10 + remainder;
        temp = temp / 10;
    }

The reverse of the number is found using the above logic and is stored in the reverse named variable.

    if (reverse == num){
        printf("%d is a Palindrome.", num);
    }
    else{
        printf("%d is not a Palindrome.", num);
        return 0;
    }

If the value of the reverse is equal to the value of the initial integer, then the number is palindrome, otherwise the number is not palindrome. 

The output is displayed to the screen using printf() function.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. 

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen.  

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

% – It is known as Modulus Operator and provides remainder after division.

// – Used for Commenting in C. 

Conclusion

I hope after going through this post, you learn how to check whether a number is palindrome or not using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *