June 16, 2021

Check the number is palindrome or not

Detailed question : Design and develop a C program to find the reverse of an integer number and check whether it is PALINDROME or NOT with suitable messages. Ex: Num: 1234, Reverse: 4321, Not a Palindrome.

Sounds complicated? No, will think about the logic. From user, we will get a number. Then we need to reverse the number. How to reverse the number? we need to get each digit from end. example: If the user has entered 123 then we should get 3, 2 then 1.
Can you think of remainder ? If we divide the number by 10, remainder will be the last digit. In this case, remainder is 3. After this we should use the number 12. get remainder, which is 2. Confusing a lot? Do not worry. Then check the below logic first.

#include <stdio.h>

int main() {
   
  int num;
  printf("Enter an integer. Press enter after entering the number.\n");
  scanf("%d", &amp;num);
    
  int revNumber = 0;	
  int tempNum = num;
  
  int temp = 0;
  while(tempNum>0){
	int remainder = tempNum%10; 
	revNumber = revNumber *10 + remainder;
	tempNum = tempNum/10;	

  }	  
  if(num == revNumber){
	    printf("Number is a palindrome");
  }else{
	    printf("Number is not a palindrome");
  }	  
  
  return 0;
}


	

Create tempNum variable and assign the value of num to this variable. Crete a new int variable revNumber.
In the while loop, get the remainder of tempNum. add the value revNumber *10 + remainder; Divide the tempNumber by 10. This process will repeat until tempNum becomes less than or equal to zero.

Will debug the while loop for better understanding. Even you can print the value of tempNum, revNumber in the while loop to get more clarity.

Assume that user has entered the number 123.
While loop is executing for the first time. tempNum = 123 Its greater than zero.
remainder = 123%10 = 3
revNumber = revNumber *10 + remainder; Initially revNumber is zero.(Line no: 10). So, revNumber = 0*10 + 3 = 3
tempNum = tempNum/10; So, tempNum = 123/10 = 12 Since tempNum is integer, It will get only 12, not 12.3

While loop is executing for the second time. tempNum = 12, Its greater than zero.
remainder = 12%10 = 2
Remember: At this time, value of revNumber = 3
revNumber = revNumber *10 + remainder; 3*10 +2 = 32
tempNum = tempNum/10; So, tempNum = 12/10 = 1 since tempNum is integer.

While loop is executing for the third time. tempNum = 1, Its greater than zero.
remainder = 1%10 = 1
Remember: At this time, value of revNumber = 32
revNumber = revNumber *10 + remainder; 32*10 +2 = 321
tempNum = tempNum/10; So, tempNum = 1/10 = 0 since tempNum is integer.

While loop is executing for the forth time. tempNum = 0, Its not greater than zero. So, while loop will not execute now. Control will come out of while loop. Now what is the value of revNumber? Check above statements. Its value is 321.
Now compare this value with the value of num. If its equal, display : Number is a palindrome Or else display : Number is not a palindrome

Leave a Reply

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