Sum of squares of Fibonacci numbers in C++

C++Server Side ProgrammingProgramming

Fibonacci series is a mathematical sequence of number which starts from 0 and the sum of two numbers is equal to the next upcoming number, for example, the first number is 0 and the second number is 1 sum of 0 and 1 will be 1

F0=0, F1=1

And

Fn=Fn-1+Fn-2,
F2=F0+F1
F2=0+1
F2=1

then when we add number 1 and 1 then the next number will be 2

F1=1, F2=1

And

Fn=Fn-1+Fn-2,
F3=F1+F2
F3=1+1
F3=2

Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

We have to find the square of the fuel energy series and then we have to sum it and find the result

Input :4
Output:15
Explanation:0+1+1+4+9=15
forest we will solve Fibonacci numbers till N then we will square them then at them

Example

 Live Demo

#include <iostream>
using namespace std;
int main(){
   int n=4, c;
   int first = 0, second = 1, next;
   int sum =0;
   for ( c = 0 ; c < n+1 ; c++ ){
      if ( c <= 1 )
         next = c;
      else{
         next = first + second;
         first = second;
         second = next;
      }
      sum+=next*next;
   }
   printf("%d",sum );
   return 0;
}

Output

15
raja
Published on 24-Oct-2019 14:38:17
Advertisements