Palindrome Number using Recursion in C

Palindrome-Number-using-Recursion-in-C-Programming

Palindrome Number using Recursion in C Program Description:

Write a Program to check whether the number is a Palindrome Number using Recursion in C programming language. The program will accept a number from the user and verifies if it is a palindrome number.

What is a Palindrome Number:

When a number is a palindrome number, It means that when it is reversed, it should still be the same as the original number.

number == reversed(number);

Example Input and Output:

Input:

Enter a Number: 1234321

Output:

Given number 1234321 is a Palindrome Number

Prerequisites:

We are going to use functions and recursion in the program. So please go through the below articles to better understand the program.

Palindrome Number using Recursion in C Algorithm/Explanation:

  1. Start the program and take an integer number from the user and store it in number variable.
  2. We won’t allow a negative number, So check for negativity and display the error message if the number is a negative number. Use the if( number <= 0) condition.
  3. If the number is a palindrome, Then the original number and reversed number will be the same. So we will first calculate the reverse of the number and then compare it with the original number.
  4. So we are going to create two functions. One function is to reverse the number ( reverseNumber) and the other function is to check the palindrome ( checkPalindrome).
  5. We use the reverseNumber function, To get the reversed value of the number. The reverseNumber function takes two arguments and returns the Reversed number. We have already discussed the Reverse Number Program using Recursion in earlier posts.
    • We use the modulus operator ( number%10) to get the last digit of the number and then call the reverseNumber function recursively and pass the number by removing the last digit ( We can remove the last digit using the number/10 division operation)
  6. The checkPalindrome function takes the given number( number) as the input and check whether the number is palindrome or not.
    • The checkPalindrome function first gets the reversed number by calling the reverseNumber function. Then it will compare the reverse version with the original value. – number == <strong>reverseNumber</strong>(number, 0). If both the values are equal, Then the number is a palindrome. So the checkPalindrome function returns 1.
    • The checkPalindrome function returns an integer value, We can also return the Boolean values such as true or false. But don’t forget to include the stdbool.h header file.
  7. Call the isPalindrome function from the main function and display the results based on the return value.

If you observe the function calls, The main() function is calling checkPalindrome function and the checkPalindrome function is calling the reverseNumber function.

📢 Function Call Flow:

main() —> checkPalindrome() –> reverseNumber()

Palindrome Number using Recursion in C Program:

We used two functions(except main) in the above program, The reverseNumber and checkPalindrome functions. Here are the Prototype details of these functions.

The reverseNumber function:

  • int reverseNumber(int number, int rev_num);
  • Function_name: reverseNumber
  • Argument_list: (int, int) – Takes two integer arguments.
  • Return_Value: int – Returns the reversed number.

The checkPalindrome function:

  • int checkPalindrome(int number);
  • Function_name: checkPalindrome
  • Argument_list: (int) – Takes one integer as an argument.
  • Return_Value: int – Returns 1 if the number is a palindrome, Otherwise, returns 0.

Program Output:

Compile and run the program using your favorite IDE.

Test 1: Positive Numbers:

Test 2: Positive Numbers with Zeros

Program-to-check-Palindrome-Number-using-Recursion-in-C-output

We are getting the expected results.

Test 3: Negative Numbers:

Exercise:

Rewrite the above program and make the following changes

  • The checkPalindrome function should return boolean values instead of the integer value.
  • Use the static variable to calculate the reverse of the number. So change the reverseNumber program to take only one parameter and use the static variable. ( Hint: Check the Method-2 of Reverse Number using Recursion Program )

Recursion Practice Programs:

Functions Practice Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to check whether the number is Palindrome using Recursion […]

Leave a Reply