1. Engineering
  2. Computer Science
  3. this program supposes to do this quotfibonacci sequence starts with...

Question: this program supposes to do this quotfibonacci sequence starts with...

Question details

#include iostream» 10 11 using namespace std; 12 13 int fib( int n); 14 15 int sum;

17 int main) 18 ▼ 19 20 21 cout << Type the number you desire : ; cin > n cout << endl; cout << fib number of input : くく fib (n) << endl << The total sum of even number is くく sum; 23 25 26 27 28 29 int fib( int n) 30 31 32 return if return 0 if(n== 1 35 36 37 38 39- 40 41 42 43 return 1: int num fib(n-1) fib (n-2); cout <<Num- << num 〈 endl if(num % 2 = 0 ) { sum Sum+ num cout << Output = << sum << endl; 45I/ return fib(n-1)+fib (n-2); 46 47 48 return num

This program supposes to do this

"Fibonacci sequence starts with 0 and 1 where each fibonacci number is a sum of two previous fibonacci numbers. Given an integer N, find the sum of all even fibonacci numbers."

Example) input = 12

output = 10 because 2 + 8 = 10

In this program, n is for nth fibonacci number and sum is for sum of all even numbers that question asked for.

However, I always got the 0 for the sum value ( nth number prints out correct all the time).

I don't know why this happens.

Type the number you desire: 7 1 2 Output 2 um 1 um3 um1 2 Output 4 um 5 um 2 Output -6 Num1 Num3 um Output 14 um - 1 Output 16 1 um 3 Output 18 Num5 um 13 fib number of input : 13 The total sum of even number is 0 .. . Program finished with exit code 0 Press ENTER to exit console.

Can someone explain it to me why this method can't calculate the sum and also what am I suppose to fix it??

This is C++ fyi.

Thank you :)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution