Python-For Loop-(Sum of N Fibonacci numbers)
# Complete the sumOfNFibonacciNumbers function below.
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
def sumOfNFibonacciNumbers(n):
# Write your code here
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
result = sumOfNFibonacciNumbers(n)
fptr.write(str(result))
fptr.close()
In mathematics, the Fibonacci numbers commonly denoted F, form a sequence called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
F(n) = F(n-1) + F(n-2) for n > 1.
The beginning of the sequence is thus: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.
Define a function called sumOfNFibonacciNumbers which takes two parameters. The first parameter is n, which denotes the number of Fibonacci numbers. The function definition code stub is given in the editor. Calculate the sum of n Fibonacci numbers and return it.
Constraints:
1 <= n <= 100
Input Format for Custom Testing:
In the first line, n is given.
Sample Case:
Sample Input:
5
Sample Output:
7
Explanation:
In the above given sample, n is 5. The first 5 Fibonacci numbers are 0, 1, 1, 2, 3 and their sum is 0 + 1 + 1 + 2 + 3 = 7. The final output is 7.