Write a program to find the sum of the Fibonacci series using the function
Example:
Input: n = 10 Output: Sum of series of length 10 is 88
Approach
C
#include <stdio.h>int sumFibSeries(int n){int a = 0, b = 1;if (n == 1)return 0;if (n == 2)return 1;else{int sum = 1;for (int i = 2; i < n; i++){int temp = b;int fib = a + b;b = a + b;a = temp;sum += fib;}return sum;}}int main(){int n;printf("Enter a number: ");scanf("%d", &n);int sum = sumFibSeries(n);printf("Sum of series of length %d is %d ", n, sum);return 0;}
Java
import java.util.Scanner;public class Main{public static void main(String[] args) {System.out.println("enter the number");Scanner sc = new Scanner(System.in);int num = sc.nextInt();System.out.println("sum of fibonacci of "+ num + " is "+fibonaccisum(num));}public static int fibonaccisum(int n){int a = 0, b = 1;if (n == 1)return 0;if (n == 2)return 1;else{int sum = 1;for (int i = 2; i < n; i++){int temp = b;int fib = a + b;b = a + b;a = temp;sum += fib;}return sum;}}}
No comments:
Post a Comment