Reverse a number using Recursion in C Language

Spread the love
Program-to-Reverse-a-number-using-Recursion-in-C-language

Reverse a number using Recursion in C Program Description:

Write a Program to Reverse a number using Recursion in C Programming Language. The Program will accept an input number from the user and reverses it using recursion. We have already looked at the Iterative method to reverse a number in our earlier articles.

Excepted Program Input and Output:

Input:

Enter a Number to Reverse: 5738

Output:

Reversed Number is : 8375

Pre-Requisites:

We are going to use the functions and recursion in the following program. If you need to refresh your knowledge please go through the following articles, where we covered the above topics.

Reverse a number using Recursion in C Algorithm/Explanation:

  1. Take the user input and store it in the variable named num.
  2. We are not going to allow negative numbers, So display the error message if the user enters a negative number. Check if the number is a negative number using num <= 0 condition.
  3. Define a function named reverseNumber. The reverseNumber function, Takes two values as input, they are num and reverse number. Pass the input number num as the first argument and zero as the second argument – reverseNumber(num, 0);
    • The reverseNumber function generates the Reverse value of the provided number using recursion and returns it to the caller.
    • The base condition for the recursion is if(num == 0). So the recursion will continue until the num reaches zero.
    • At each function call, update the rev_num to create the reverse value – rev_num = (rev_num * 10) + (num % 10);
    • Then make a recursive call to reverseNumber and pass the num/10 and rev_num. – reverseNumber(num/10, rev_num);
    • Note: We can also use the Static rev_num variable instead of passing it to every call like above. ( We will write the static number method program as well in the next section).
    • The recursion will continue until the num reaches the base condition.
  4. Call the reverseNumber function from the main function. Pass the num and zero(0) as the parameters – reverseNumber(num, 0);. The reverseNumber function returns reversed number, Then Store the return value in reversedNum variable
  5. Finally, Display the results to the user.

Reverse a number using Recursion in C Program:

Let’s convert the above algorithm into the C code.

The prototype details of the reverseNumber function.

  • int reverseNumber(int num, int rev_num)
  • Function_name: reverseNumber
  • Arguments_list: (int, int)
  • Return_value: int

📢 Here we are passing the rev_num to all recursive calls, We can also use the Static variable for the rev_num parameter so that it will live through the function calls.

Program Output:

Compile and Run the program using your IDE. We are using the GCC compiler here. Let’s try few test cases and see if our program is working fine.

Test Case 1: When the user provides the Positive Numbers:

Test Case 2: Positive Numbers with Zeros:

Reverse-a-number-using-Recursion-in-C-program-output

As we can see from the above output, We are getting the appropriate results from the program.

Test Case 3: Negative Numbers:

The program generates the error message Error: Please enter a positive Number when the negative number is provided by the user.

Method 2: Reverse a number using Recursion in C using Static Variable:

Here is the program to Reverse a number using recursion using a static variable in C programming language.

The main difference between the above two methods is, Instead of passing the rev_num to reverseNumber function, We are using the static variable.

📢 The static variables will be created at the start of the program and will be destroyed when the program terminates. They will live through the function calls. This means unlike the local variables(auto), The static variables will not be freed once the function terminates.

We will dive deep into the variable Life and Scopes in the Storage Classes in the C language section.

Program Output:

Let’s compile and run the program.

$ gcc reverse-number-recursive-method-2.c

The above command generates the a.out file. Run the executable file.

The program is giving the desired results.

Recursion Practice Programs:

Functions Practice Programs:

Venkat

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...

Leave a Reply