Adsense 728 * 15

Search

Custom Search

Program to count number of Palindrome number between given range

Write a C,C++ program to count number of palindrome number between given range.

Let's say given number range is 10,30. Between 10 and 30 there are two palindrome number 11 and 22.

LOGIC

1. Take start and end input.

2. Run a loop from start and end.

3. Reverse each number and compare to original number, if both are same then it's palindrome.


Program to reverse a number

Program to check whether number is palindrome or not.

Program to count number of Palindrome number between given range

#include <stdio.h>

/* Reverse and compare .*/

int reverse(int num){
 
   int rev,temp,sum=0;
 
   temp = num;
 
   while(temp>0){
  
      rev = temp%10;
      sum = sum*10 +rev;
      temp = temp/10;
  }
 
  /* If reverse number is equal to original number
     then it's palindrome. */

   if(sum==num){
  
       return 1;
     }else {
       return 0;
     }
  }


int main()
{
     
     int start,end,j,count=0;
    
     printf("Enter start and end range");
 
     scanf("%d",&start);
     scanf("%d",&end);

     /* Iterate from start to end. */
     
     for(j=start;j<=end;j++){
      
       if(reverse(j)){
 
        // Increment count variable.

         count++;
        }
      
     }
     
     printf("%d\n",count);
    
     return 0;
}


Output:


Enter start and end range 10  30

Total palindrome number is 2

No comments:

Post a Comment