473,978 Members | 18,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reversing digits of an integer using recursion

17 New Member
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
15 19161
Banfa
9,065 Recognized Expert Moderator Expert
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,585 Recognized Expert Moderator MVP
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 Recognized Expert MVP
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,585 Recognized Expert Moderator MVP
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 New Member
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,585 Recognized Expert Moderator MVP
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
10413
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; else{ s++; return 1 + count_length(s);
5
2170
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
14806
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 errors:
13
11660
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
6978
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 ive written the program it runs but im not having the correct values..here is the code: #include<iostream.h> #include<conio.h>
6
8668
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. Please help me, #include "stdio.h" struct node { int n;
2
4401
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
2
1722
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 nTimes. I cannot use a for or while loop, just recursion. How would I go about doing this? all i know how to do is get it started and use recursion to call function again. def printNtimes(aString,nTimes): I also know how to do it using recursion...
3
2975
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
10356
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11413
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6561
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5160
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.