Skip to main content

TCS

TCS OA-23 2024

Problem Description

You are given a positive integer N. Your task is to calculate and return the sum of the first N terms in the Fibonacci series.

The Fibonacci series is defined as:

  • F(0)=0
  • F(1)=1
  • F(n)=F(n−1)+F(n−2) for n≥2

The sequence of the first few Fibonacci numbers is: 0, 1, 1, 2, 3, 5, 8, 13, ...

Input Format

A single integer N, the number of terms to sum from the Fibonacci series.

Output Format

An integer representing the sum of the first N terms in the Fibonacci series.

Constraints

  • 1 ≤ N ≤ 10⁴

Example

Sample Input:

6

Sample Output:

12

Explanation:

The first 6 terms of the Fibonacci series are: 0, 1, 1, 2, 3, 5.
Their sum is 0+1+1+2+3+5=12.