program for palindrome in c

C Program to check whether a number is palindrome or not.                            


 

Code: 

#include <stdio.h>
#include<conio.h>
int main()
{
    int digit,n;
    int rev=0,temp;
    printf("Enter a Number:");
    scanf("%d",&n);
    temp=n;
    while(temp!=0)
    {
        digit=temp%10;
        rev=rev*10+digit;
        temp=temp/10;
    }
    if(rev==n)
    {
        printf("Entered Number is a Palindrome Number");
        
    }
    else
    {
     printf("Entered Number is not a Palindrome Number");   
    }
    
    
        getch();
    return 0;
}

 

Sample Input and Output: 

Enter a Number:121                                                                                                                   
Entered Number is a Palindrome Number

 In this article,we are going to see how we can write program to check whether the entered number is palindrome or not.So Let's start

What is Palindrome Number ?

It is a number that is same when written in backwards or forwards for eg 121,101 etc because if we reverse 121 then it will be still 121.Let's take one non-palindrome so all doubt will be cleared.193 if we reverse this number then it will return 391 as we can see that after reversing integer integer then we can see that number has been changes from 193 to 391,Therefore we can say that it is not a palindrome number.If you still have one any doubt then let me known in the comment section.

Now Let's understand the program 

In the  first two line of our code we have included our header file which will help us to use some of the inbuilt function like printf(),scanf() etc.Whose who don't known what is built-in function then I would like to tell them that these are function which are already defined by someone so that anyone can directly use it ,now need to implement it again.So it improves  efficiency of the application.

In the next line of our code,we have declared two variable  one with name digit and other with name n where n will be used to take user input i.e. number which we want to find whether it is palindrome or not and I will tell you the use of digit variable further.

In the next line line we have declared two variable temp rev and temp and also initialized rev with 0,whose who are confused why we have given variable name rev,temp then I would like to tell them that rev,temp is short form of reverse,temporary respectively.

In the next line of code we have simply printed a 'Enter a Number:' statement which will help user to get to known that Program to Waiting for our input and we need to provide some input then only the program will proceed because after this print statement we have wrote scanf() which actually pause the execution and resume the execution only,when the input is provided.

In the next line we have assigned the value of n(input which is provided by user) to temp variable,this is because we don't want to alter the value of n because we are going to use it for comparing the computed value with actual input if same then the entered number is palindrome otherwise not!I known most of you might not understand the previous line so don't be panic stay Tuned you will understand everything.

In the next line we have defined one while loop with some condition i.e while(n!=0) the meaning of this statement is execute the statement  inside the while loop till the value of n is not 0 once the value of n become 0 then the control of program will come outside the while loop and statement after the loop will be executed.

Let's understand while loop iteration by iteration since the main logic of finding palindrome is inside the loop so let's get into it.

The  sample value of n = 191 

temp = 191 (∵ temp = n)

1st Iteration

while(191!=0)True  Actual Condition - while(temp!=0) // The condition satisfied 191 is not equal to 0 therefore the cursor will go inside the while loop

digit = 191 % 10;  Actual Condition - digit = digit % 10 // In this line ofcode we are calculating the 191 modulo 10 mean the remainder which we should after dividing 191 / 10. 

rev = 0 * 10 + digit; Actual Condition - rev = rev * 10

temp = 191 / 10;  Actual Condition - temp = temp / 10



 




  



    

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

  Previous Next

Post a Comment

Previous Post Next Post