C Program to Check Palindrome Number Using Functions

Write a C Program to Check Palindrome Number Using Functions, and also find the palindrome number in the given range. Previously we had developed a C program to check whether the given number is a Palindrome number or not? Now, we will do the same using C functions.

Palindrome number:- If the Reverse of a number is equal to the same number then the number is called a palindrome number.

Example:-
5225 = 5225 So, 5225 is a palindrome number.
123 = 321 So, 123 is not a palindrome number.

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming. The function is a small program that is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

The C program is made of one or more pre-defined/user-defined functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

Function to check number is Palindrome or not

The process to check number is a palindrome number or not,
1) Take a number
2) Find the reverse of the number
3) If reverse and the actual number is the same then it a palindrome number
4) Else it is not a palindrome number

int checkPalindrome(int number)
{
  // declare variables
  int temp, remainder, rev=0;

  // copy of original number
  temp = number;

  // loop to repeat
  while( number!=0 )
  {
     // find last digit
     remainder = number % 10;

     // calculate reverse
     rev = rev*10 + remainder;

     // remove last digit
     number /= 10;
  }

  /* if reverse is equal to the
   * original number then it is a 
   * palindrome number else it is not.
   */
  if ( rev == temp ) return 0;
  else return 1;
}

Take a copy of the original number because at the end we need to compare the original number and calculated value. If the reverse is equal to the original number then it is a palindrome number else it is not a palindrome number.

C Program to Check Palindrome Number Using Functions

#include<stdio.h>
int checkPalindrome(int number)
{
  int temp, remainder, rev=0;
  temp = number;

  while( number!=0 )
  {
     remainder = number % 10;
     rev = rev*10 + remainder;
     number /= 10;
  }

  if ( rev == temp ) return 0;
  else return 1;
}

int main()
{
  int number;

  printf("Enter the number: ");
  scanf("%d", &number);

  if(checkPalindrome(number) == 0)
  printf("%d is a palindrome number.\n",number);
  else
  printf("%d is not a palindrome number.\n",number);

  return 0;
}

Output for different test-cases:-

Enter the number: 1234
1234 is not a palindrome number.

Enter the number: 5225
5225 is a palindrome number.

C Program to Check Palindrome Number in a given range Using Functions

#include<stdio.h>
int checkPalindrome(int number)
{
  int temp, remainder, rev=0;
  temp = number;

  while( number!=0 )
  {
     remainder = number % 10;
     rev = rev*10 + remainder;
     number /= 10;
  }

  if ( rev == temp ) return 0;
  else return 1;
}

int main()
{
  int min, max;

  printf("Enter min and max value of the range: ");
  scanf("%d %d", &min, &max);

  printf("Palindrome number in the given range are: \n");

  for(int i=min; i<=max; i++)
  if(checkPalindrome(i) == 0)
  printf("%d\t",i);

  return 0;
}

Output for the different test cases:-

Enter min and max value of the range: 50 100
Palindrome number in the given range are:
55 66 77 88 99

Enter min and max value of the range: 500 600
Palindrome number in the given range are:
505 515 525 535 545 555 565 575 585 595

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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