473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reversing digits of an integer using recursion

17
I am writing in C. I want to reverse the input integer using recursion so if the input is 456, the output should be 654. it only needs to work with positive numbers.
I've already written this function but the only output i get for any input is '0'. I know that its the same 0 from sum=0; because when i change 'sum' to any other number at the beginning, that number is outputted. its almost as if the 'if' clause is being skipped completely.
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. int reverse(int num)
  2. {
  3.      int sum,z;
  4.      sum=0;
  5.      if(num>0)
  6.        {
  7.        z=num%10;
  8.        sum=(sum*10)+z;
  9.        reverse(num/10);
  10.        }
  11.      else
  12.        return (sum);
  13. }
Aug 23 '08 #1
15 19069
leejwen
50
<Code Removed Please Read how to respond to a question>
Aug 23 '08 #2
Banfa
9,065 Expert Mod 8TB
ab12, you have a recursive function yet you never use the return value of that recursion.

The true block of you if statement never returns a value.

The only value actually returned by your function is 0 in the false block of the if statement.
Aug 23 '08 #3
ab12
17
ab12, you have a recursive function yet you never use the return value of that recursion.

The true block of you if statement never returns a value.

The only value actually returned by your function is 0 in the false block of the if statement.
That was kind of the point because eventually i want the function to return whatever the value of sum is...so since thats not working what could i do to fix this problem?
Aug 23 '08 #4
ab12
17
Remove line #11.

kind regards,

Jos
i did that but now if i enter something like 456, my output is only 6
Aug 23 '08 #5
JosAH
11,448 Expert 8TB
i did that but now if i enter something like 456, my output is only 6
I accidentally removed my message while I wanted to edit it; sorry for that.
Your algorithm isn't correct. Check it by hand, e.g. for a number 456 you
want to reverse 56 and append 4 or you want to reverse 45 and prepend 6.
Of course reversing the number 56 or 45 is the recursive step.

kind regards,

Jos
Aug 23 '08 #6
ab12
17
I accidentally removed my message while I wanted to edit it; sorry for that.
Your algorithm isn't correct. Check it by hand, e.g. for a number 456 you
want to reverse 56 and append 4 or you want to reverse 45 and prepend 6.
Of course reversing the number 56 or 45 is the recursive step.

kind regards,

Jos
ok i figured it out. i instead used a while statement. Thanks.
Aug 23 '08 #7
Banfa
9,065 Expert Mod 8TB
I have to say it sounds like you are trying to code the algorithm without actually knowing what it is. That is trying code randomly rather than knowing the algorithm that performs the operation and then writing code the implements the algorithm.

Can you write down the algorithm you are trying to solve?

Also a common gotcha with this algorithm is its ability to handle 0 digits. When you get to testing you should definitely test with a value like 1304.
Aug 23 '08 #8
Banfa
9,065 Expert Mod 8TB
Oops didn't see your last message, glad it's working but I still suggest testing with a 0 digit in the value.
Aug 23 '08 #9
ab12
17
Oops didn't see your last message, glad it's working but I still suggest testing with a 0 digit in the value.
its still works with 0 as a digit, but i was wondering, is this function still recursive if i use a while(n>0) statement? at the end of the while statement i put num=num/10;
Aug 24 '08 #10
Banfa
9,065 Expert Mod 8TB
No it is now iterative.

A function that uses a loop is iterative, that is it iterates (does the same thing again and again).

A function that calls itself is recursive, this is it recurses (calls itself again and again)

A rule of thumb is that it is possible to re-write any recursive function as an iterative function and any iterative function as a recursive one.
Aug 24 '08 #11
NeoPa
32,556 Expert Mod 16PB
Just a small, general, suggestion.

In a case like this, where the procedure itself is pretty basic, it's a good idea to take your code through a dry run.

Take a piece of paper and write out some columns for each of your variables (including one (IP) for an Instruction Pointer - to say where you've reached in the code), and then process through your code as the computer would, and reflect on the paper what is happening. You will very often find that you can see easily where the logic falls down, and get a much better understanding of what's going on in the process.

It would be perfect for a situation like this. The IP value is particularly important when working with recursive functions.

I hope this helps you and welcome to Bytes!
Aug 24 '08 #12
JosAH
11,448 Expert 8TB
Take a piece of paper and write out [ ... ]
As well as reading, pencil and paper are soooo much 20th century and outdated.
We live in a copy and paste society and we demand that it all works instantaneously.
No comprehension is needed nor wanted anymore.

kind regards,

Jos
Aug 24 '08 #13
NeoPa
32,556 Expert Mod 16PB
Just a small, general, suggestion.

In a case like this, where the procedure itself is pretty basic, it's a good idea to take your code through a dry run.

Open up Excel or any spreadsheet program and head up some columns for each of your variables (including one (IP) for an Instruction Pointer - to say where you've reached in the code), and then process through your code as the computer would, and reflect, in the spreadsheet, what is happening. You will very often find that you can see easily where the logic falls down, and get a much better understanding of what's going on in the process.

It would be perfect for a situation like this. The IP value is particularly important when working with recursive functions.

I hope this helps you and welcome to Bytes!
Aug 25 '08 #14
Sumedh
1
when u call the reverse function again.. the variable sum is every time initialized to zero..! then we dont obtain the reverse of the number !!
Feb 7 '12 #15
NeoPa
32,556 Expert Mod 16PB
It's a local variable (on the stack) so it's only used in the current call of the procedure. As such, there is simply no basis for your statement and it is not true.
Feb 7 '12 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
5
Rooro
by: Rooro | last post by:
For the List class, add a Boolean-valued function that determines whether the data items in the linked list are arranged in ascending order. How can i do this using recursion
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
13
by: Raman | last post by:
Hi All, Could any one tell me How to concatenate two strings using recursion. And also how to trim a string using recursion.(in C, offcourse) Regards, Raman Chalotra
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
6
by: Suyash Upadhyay | last post by:
Hi All, As a beginner in Computer Programming, I decided to create Linked List using recursion, but I am not getting right answer. I think it is fundamentally correct, but I am stuck. ...
2
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
2
by: wagn31 | last post by:
new to python and I have this assingment: I need to write a recursive function that takes paramaters (aString,nTimes) and it needs to print the aString parameter the number of times specified by...
3
by: hemanthappala | last post by:
hello for every body of this network,if any one konw c-program of sum of n terms using recursion please post it
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.