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: - int reverse(int num)
-
{
-
int sum,z;
-
sum=0;
-
if(num>0)
-
{
-
z=num%10;
-
sum=(sum*10)+z;
-
reverse(num/10);
-
}
-
else
-
return (sum);
-
}
15 19134 Banfa 9,065
Recognized Expert Moderator Expert
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.
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?
Remove line #11.
kind regards,
Jos
i did that but now if i enter something like 456, my output is only 6
JosAH 11,448
Recognized Expert MVP
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
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.
Banfa 9,065
Recognized Expert Moderator Expert
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.
Banfa 9,065
Recognized Expert Moderator Expert
Oops didn't see your last message, glad it's working but I still suggest testing with a 0 digit in the value.
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;
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
|
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
|
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:
|
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
|
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>
| |
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;
|
by: guneet bhatia |
last post by:
please help with fibonacci series using recursion..
|
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...
|
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
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |