Palindrome number in C Programming |Check palindrome number

In this tutorial, we will learn about palindrome number and How to check palindrome number in C programming. In this program, I use the easiest way to check palindrome number. So let’s start.

What is a palindrome number?

Before starting we have to know what is the palindrome number. If we reverse a number that is equal to the original, then this number is called a palindrome number. Suppose, our number is 121 and this reverse number is also 121. So this number is a palindrome number. Look at another example, if our number is 120 and this reverse number is 021 and we can see that the reverse number is not equal to the original number. So this is not a palindrome number.

1. How to check palindrome number using C programming?

In this program, we will check whether a number is a palindrome or not. We take the number from the user. Our user gives us a number and we have to give the user output as if this is a palindrome number or not.

Algorithm of Palindrome number:

Step 1: start the program

Step 2: Take input from the user

Step 3:Assign the input number to a temp variable

Step 4: reverse the temp variable

Step 5: Check temp==input number

Yes: print Palindrome number

No: print not a palindrome number

Step 6: End the program

Palindrome number in C Programming Code:

#include<stdio.h>
int main(){
       int num, temp, rev=0, rem;
       printf("Enter the number:");
       scanf("%d",&num);
       //assign num variable value in temp
       temp=num;
      //reverse temp variable
       while(temp!=0){
             rem=temp%10;

            rev=rev*10+rem;

            temp=temp/10;
       }
       //check reverse number is equal to original number
       if(rev==num){
         printf("This is a Palindrome number");
       }else{
         printf("This is not Palindrome number");
       }
       getch();
}

Output:

Palindrome number Code Explanation:

In this program, at first, we take 4 integer variables that are num, temp, rem, and rev. We can take input from the user and store it num variable. And we also assign the value of the num variable in the temp variable. This temp variable we use to reverse our number. Now we start a while loop and it will stop when it finds 0 in the temp variable.


Read More:

How to print Pascal’s triangle in C programming | Pascal Pyramid

Print the Fibonacci series until a given number using C programming


Then, we extract the last digit using the modulus operatore and we store the last number in the rem variable. After that, our main process is to start with the rev variable. Here at first, we initialized this variable using 0. And we multiply the current value of the rem variable by 10 and add the rem variable value.

At last, we exclude the last. In this way, our while loop works until it found 0 in temp. Finally, our reverse number will be stored in the rev variable.

Now we have to check our reverse number and original number. If they are equal then we can print this as a palindrome number. Otherwise, we print not a palindrome number.


Comments

Leave a Reply

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