answersLogoWhite

0


Best Answer

//Function that reverses a given number

int reverse(int num)

{

static int sum,base =1;

sum=0;

if(num>0)

{

reverse(num/10);

sum += (num%10)*base;

base*=10;

}

return sum;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Assuming you want to reverse the digits of the number:

numToReverse <- the number we want to reverse

revNum <- new number as numToReverse is reversed

while numToReverse is not 0

// shift digits left

revNum = revNum * 10

// tack on rightmost digit of numToReverse

revNum = revNum + (numToReverse modulus 10)

// shift digits right

numToReverse = numToReverse / 10

// numToReverse is now 0

// revNum is now the reverse of (the original value of) numToReverse

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<stdio.h>

#include<conio.h>

int rev(int n);

void main()

{

int n;

clrscr();

printf("Enter the n value\n");

scanf("%d",&n);

printf("The reverse number is %d",rev(n));

getch();

}

int rev( int n)

{

int re;

int dig;

if (n==0)

{

return 0;

}

dig=n%10;

n=n/10;

re=digit+10*re(n);

return re;

}

return 1;

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Call your reversal function reverse().

If you have an empty string, return the empty string (this is the condition for ending recursion). You can also end the recursion when your string has a length of a single character (return the single character), but you must still account for the possibility of empty strings.

Otherwise, separate the string into two parts: for example, the first letter, and the remaining string (see Note 1). Return the reverse() of the second part, joined to the reverse() of the first part.

For example, if your string is "abc", your function would have to return reverse("bc") + reverse("a"). reverse("bc") will, in turn, result in reverse("c") + reverse("b"), so the result will be "cba".

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Do it with recursion!

void rev (int n)

{

putchar ('0'+n%10);

n/=10;

if (n) rev (n);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Yes, do.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the algorithm for reverse a given number recursively?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


Write an algorithm and draw a corresponding flowchart to search a number in the given list of numbers and also display its position?

please give me an algorithm and a corresponding flow chart that displays list of numbers from 1 to 20.


How do you convert a number that consist of alphabet and number in reverse order?

To reverse a number, first convert the number to a string, then reverse the string. Given your number consists of alphanumeric characters, the number must already be a string so simply reverse the string: #include&lt;string&gt; using std::string; string reverse (const string&amp; s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout &lt;&lt; "Enter a number: "; string s {}; std::cin &gt;&gt; s; std::cout &lt;&lt; "The number in reverse is: " &lt;&lt; reverse (s); }


How do you write a program that outputs a given characters in reverse?

write the javascript code to display the reverse no. of given no. (e.g. 247 reverse of 742)


How do you choose between recursion and iteration?

Some problems cry out for recursion. For example, an algorithm might be defined recursively (e.g. the Fibonacci function). When an algorithm is given with a recursive definition, the recursive implementation is straight-forward. However, it can be shown that all recursive implementations have an iterative functional equivalent, and vice versa. Systems requiring maximum processing speed, or requiring execution within very limited resources (for example, limited stack depth), are generally better implemented using iteration.

Related questions

Fox pro program to reverse the given number?

reverse programe in fox pro


What is complsexity of an algorithm?

Complexity of an algorithm is a measure of how long an algorithm would take to complete given


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


Enter a number and draw a flow chart to get the reverse of the given number and the number is 435?

123


How can you generate a palindrome from a given number?

You write the number and then follow it with the digits in reverse order.


Write an algorithm to check whether the given number is odd or even?

Type your answer here... i think we should first enter 1 number then check it


Write an algorithm and draw a corresponding flowchart to search a number in the given list of numbers and also display its position?

please give me an algorithm and a corresponding flow chart that displays list of numbers from 1 to 20.


What can the reverse lookup be used to find?

The reverse lookup is a telephone number data base. Instead of looking up a phone number based on a given name, a name can be looked up based on a given phone number. It is useful when receiving phone calls from an unknown phone number.


Why algorithm needs to solve programming problem?

This is the definition of an algorithm - a list of orders of how to solve a given programming problem.


Is there an algorithm that yields only prime numbers?

What exactly do you mean "yields only prime numbers"? If you mean a formula that when given the numbers n=1, 2, 3, ... and so on generates the nth prime number (or a different prime number for each n) then no. If you mean an algorithm whereby a number can be tested to be a prime number then yes. (Using this prime_test algorithm, a simple algorithm can be written that would supply numbers one at a time to it and use its result to decide whether to yield the tested number or not, only yielding those numbers which pass the test.)


What name is given to prime numbers that you can reverse to make another prime number?

A mirror prime.


How do you convert a number that consist of alphabet and number in reverse order?

To reverse a number, first convert the number to a string, then reverse the string. Given your number consists of alphanumeric characters, the number must already be a string so simply reverse the string: #include&lt;string&gt; using std::string; string reverse (const string&amp; s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout &lt;&lt; "Enter a number: "; string s {}; std::cin &gt;&gt; s; std::cout &lt;&lt; "The number in reverse is: " &lt;&lt; reverse (s); }