help with

i keep getting an output that says the exit code is zero. I don't know if im doing it correctly.

the question is:
1. Implement a recursive method fibo that takes an integer num as a parameter and returns the nth Fibonacci number.
2. Implement a recursive method mult that takes two integer parameters, x and y and returns their product.
3. Write a program that tests the two methods above.



#include <iostream>
using namespace std;

int fibo(int n)
{
if (n == 0 || n == 1)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int mult(int x,int y)
{
if (y == 1)
{
return x;
}
return x*mult(x, y - 1);
}

int main()
{
cout << "10th fiboncci number:" << fibo(10) << endl;
cout << "reverse of 10567:";
cout << "10*5=" << mult(10, 5) << endl;
return 0;
}
Are you saying nothing is printing for you?
This is what prints for me:
10th fiboncci number:89
reverse of 10567:10*5=100000


https://oeis.org/A000045
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...
Your sequence is a bit off. Your code is saying that the 0th fibonacci number is 1, and the 1st fibonacci number is also 1, meaning that the 2nd fibonacci number is 2, which is not the standard sequence.

The standard sequence either has the 0th element being 0, and 1st element being 1, or the 1st and 2nd element being 1, depending on notation.

Besides that, I don't see why you have "reverse of 10567" written, because finding the reverse of an integer isn't in the question you gave.

Your mult function fails for mult(1, -1), mult(-3, 2) and similar, but I'm not sure if that's beyond the scope of what you're supposed to do.
Last edited on
the exit code is zero.
^^ this is a normal program execution message. How did you run the program (exactly).
Its possible to make shorty programs that just execute and exit so fast you can't see the output when executed certain ways (typically double click in a gui, but there are other ways to have the issue).
try putting a bogus cin statement at the end of your program to stop it until you type something to exit.


Last edited on
PLEASE learn to use code tags, they make it easier to read and comment on your code.

You can edit your post and add them.

http://www.cplusplus.com/articles/jEywvCM9/

Your Fibonacci function's corner case is wrong:
1
2
3
4
5
6
7
8
9
10
11
unsigned long Fibonacci(unsigned long n)
{
   if (n < 3)
   {
      return 1;
   }
   else
   {
      return (Fibonacci(n - 2) + Fibonacci(n - 1));
   }
}

Why use unsigned? There are no negative numbers in the Fibonacci series, using an unsigned long (or int or even long long) allows you to calculate more, larger numbers in the series before there is integer wrap-around.

There is no check for passing a "wrong" value into the function, passing zero or a negative number that gets wrapped around to a positive value.
Last edited on
i keep getting an output that says the exit code is zero

That is telling you the program exited normally. That is good.

Any number other than zero, usually a negative number, can indicate there was an error with the program.

If you are running your program by starting within an IDE, such as Visual Studio, the text telling you how the program exited is added by the IDE. Run your program from a regular command prompt and you will only see your program's output.

Your code (properly formatted, with code tags):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int fibo(int n)
{
   if (n == 0 || n == 1)
      return 1;
   return fibo(n - 1) + fibo(n - 2);
}
int mult(int x, int y)
{
   if (y == 1)
   {
      return x;
   }
   return x * mult(x, y - 1);
}

int main()
{
   cout << "10th fiboncci number:" << fibo(10) << endl;
   cout << "reverse of 10567:";
   cout << "10*5=" << mult(10, 5) << endl;
   return 0;
}

Output when run from the Visual Studio IDE:
10th fiboncci number:89
reverse of 10567:10*5=100000

C:\Programming\My Projects 2019\cplusplus\Release\Project4.exe (process 5332) exited with code 0.
Press any key to close this window . . .

Output in a "regular" command prompt:
Microsoft Windows [Version 10.0.17763.437]
(c) 2018 Microsoft Corporation. All rights reserved.

D:\>"C:\Programming\My Projects 2019\cplusplus\Release\Project4.exe"
10th fiboncci number:89
reverse of 10567:10*5=100000

D:\>
I fixed the function corner and it is running the code. thank you
Topic archived. No new replies allowed.