What is a palindrome?
Palindrome means a number which remains same even if we reverse all its digits.
For example: 121 is a palindrome i.e. when we reverse its digits we get 121, whereas 123 is not a palindrome.
First of all, we will define four variables in our programming
int n, num, digit, rev=0; |
The 'n' will take the number input which we want to check if that is palindrome or not.
printf("\n please enter the value on n="); scanf("%d", &num); | |
Then we will assign the number value of 'n' to the variable 'num' for the ease of next codes to execute.
n=num;
Then we will use a while loop to make sure that the number is not equal to zero (0).
while(num!=0)
Now under the given while loop, we will execute our main formula for the program to check if the number is palindrome or not.
The first line we will find out the remainder of the input number 'n' and assign that to variable 'digit'
digit=num%10;
Now in the next line, we will use the 'rev=0' variable and multiply it with 10 and add the remainder which we got above the line of code.
rev=(rev*10)+digit;
Now we will divide the 'num' with 10 and find the quotient and assign that to 'num ' again.
num=num/10;
Now we will check is 'n' equals to 'rev' by using a if-else loop and if it satisfies the condition it will be printed as a palindrome.
if(n==rev) printf("\npalindrome"); |
and if the condition does not satisfy then the output will be printed as 'not palindrome'.
else
printf("not palindrome"); |
For example, let us take a number 121.
Now following the above steps, we will see its remainder with 10 is found and assigned to 'digit'. So it will be 1. Now that rev=0 will be multiplied by 10 and added with the number, therefore rev will be 0, now num will divide by 10 so new 'num' will be 12.
Following all the above steps remainder with 10 is 2 which is multiplied by 10 and added with variable 'digit' so, 'rev' will now be (1*10)+2 i.e 12. Now, 'num' divided by 10 gives 1, again its remainder with 10 will be 1 only so new 'rev' is (12*10)+1 i.e 121. again when we divide 'num' with 10 it will be 0 so our while loop will stop.
Now as our input 'n' and final 'rev' is both 121 so it will show output as Palindrome.
Another example, let us take a number 123.
Now following the above steps, we will see its remainder with 10 is found and assigned to 'digit'. So it will be 3. Now that rev=0 will be multiplied by 10 and added with the number, therefore rev will be 0, now num will divide by 10 so new 'num' will be 12.
Following all the above steps remainder with 10 is 2 which is multiplied by 10 and added with variable 'digit' so, 'rev' will now be (3*10)+2 i.e 32. Now, 'num' divided by 10 gives 1, again its remainder with 10 will be 1 only so new 'rev' is (32*10)+1 i.e 321. again when we divide 'num' with 10 it will be 0 so our while loop will stop.
Now as our input 'n' is 121 and final 'rev' is 321 since both are unequal it will show output as Not Palindrome.
C Program to check if a number is palindrome or not.
Video Tutorial
0 Comments