Write a program to reverse a number using recursion

Tech Interview Write C and Java programs to find the largest element in an array using recursion.

Write a program to reverse a number using recursion

Declare recursive function to find reverse of a number First let us give a meaningful name to our function, say reverse. The function computes reverse of number, hence it must accept an integer parameter.

So let us update our function declaration reverse int num. Finally, the function computes and returns an integer which is reverse of given number. After looking on above points function declaration to find reverse of a number is - int reverse int num ; Logic to find reverse of number using recursion Step by step descriptive logic to find reverse of a number.

Multiply reverse variable by Find the last digit of the given number. Add last digit just found to reverse. Divide the original number by 10 to remove last digit, which is not needed anymore.

Repeat above four steps till the number becomes 0 and we are left with the reversed number in reverse variable. Here also we will use the above four steps to find reverse using recursive approach with the given base condition: This is because we are using log10 function and logarithm is only defined for positive values.

Hence, to make it work for negative values we have to make little modification in our logic. Program to find reverse of a negative number using recursion To find reverse of negative number use the logic for positive number reverse which I mentioned above.

In addition check if the number is negative then convert it to positive and then pass it to reverse function. One more condition you need to check, if number was negative then convert the reversed number to negative.This article will disclose, Python Program to Reverse a Number using Python While Loop, Functions and Recursion.

To reverse a number using String operations, we first convert the int value to a String using the static method, valueOf(int) of the String class.

Next, we extract the characters of the String from the right, one by one and append it to the result, which in the beginning of the program . Reverse a given number using Recursion: In this program, we are calling a user defined function “reverse_function”, the function is calling itself recursively.

Java program to calculate the sum of N numbers using arrays, recursion, static method, using while loop. Here is the complete Java program with sample outputs. You can learn more tutorials here and Java interview questions for beginners.

write a program to reverse a number using recursion

For example if user enter as input then is printed as output. We use modulus(%) operator in program to obtain the digits of a number.

To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left. Reverse a string using recursion Write a program for palindrome using recursion Binary search through recurssion using c program Reverse a number using recursion in c program Big list of c program examples.

Email This BlogThis!

write a program to reverse a number using recursion

Share to Twitter Share to Facebook Share to Pinterest.

12B: Recursion and Recursive Algorithms - CSM