# Sum of n fibonacci numbers

Here given code implementation process.

```
// C Program
// Sum of n fibonacci numbers
#include <stdio.h>
//Calculating the sum of n fibonacci numbers
void sum_fibonacci_number(int n)
{
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
int auxiliary[n];
long long int sum = 0;
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
//Set initial sequence
auxiliary[0] = 0;
auxiliary[1] = 1;
//Calculate the sum of n fibonacci numbers
for (int i = 0; i < n; ++i)
{
if (i > 1)
{
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1];
}
// Add fibonacci number
sum += auxiliary[i];
}
printf("\n Sum of %d fibonacci number is %lld", n, sum);
}
}
int main()
{
//Test case
sum_fibonacci_number(7);
sum_fibonacci_number(10);
return 0;
}
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
// Java Program
// Sum of n fibonacci numbers
class MyMaths
{
//Calculating the sum of n fibonacci numbers
public void sum_fibonacci_number(int n)
{
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
int[] auxiliary = new int[n];
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
//Set initial sequence
auxiliary[0] = 0;
auxiliary[1] = 1;
//resultant variable
long sum = 0;
//Calculate the sum of n fibonacci numbers
for (int i = 0; i < n; ++i)
{
if (i > 1)
{
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1];
}
// Add fibonacci number
sum += auxiliary[i];
}
System.out.print("\n Sum of " + n + " fibonacci number is " + sum);
}
}
public static void main(String[] args)
{
MyMaths obj = new MyMaths();
//Test case
obj.sum_fibonacci_number(7);
obj.sum_fibonacci_number(10);
}
}
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
//Include header file
#include <iostream>
using namespace std;
// C++ Program
// Sum of n fibonacci numbers
class MyMaths
{
public:
//Calculating the sum of n fibonacci numbers
void sum_fibonacci_number(int n)
{
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
int auxiliary[n];
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
//Set initial sequence
auxiliary[0] = 0;
auxiliary[1] = 1;
//resultant variable
long sum = 0;
//Calculate the sum of n fibonacci numbers
for (int i = 0; i < n; ++i)
{
if (i > 1)
{
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1];
}
// Add fibonacci number
sum += auxiliary[i];
}
cout << "\n Sum of " << n << " fibonacci number is " << sum;
}
}
};
int main()
{
MyMaths obj = MyMaths();
//Test case
obj.sum_fibonacci_number(7);
obj.sum_fibonacci_number(10);
return 0;
}
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
//Include namespace system
using System;
// C# Program
// Sum of n fibonacci numbers
class MyMaths
{
//Calculating the sum of n fibonacci numbers
public void sum_fibonacci_number(int n)
{
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
int[] auxiliary = new int[n];
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
//Set initial sequence
auxiliary[0] = 0;
auxiliary[1] = 1;
//resultant variable
long sum = 0;
//Calculate the sum of n fibonacci numbers
for (int i = 0; i < n; ++i)
{
if (i > 1)
{
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1];
}
// Add fibonacci number
sum += auxiliary[i];
}
Console.Write("\n Sum of " + n + " fibonacci number is " + sum);
}
}
public static void Main(String[] args)
{
MyMaths obj = new MyMaths();
//Test case
obj.sum_fibonacci_number(7);
obj.sum_fibonacci_number(10);
}
}
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
<?php
// Php Program
// Sum of n fibonacci numbers
class MyMaths
{
//Calculating the sum of n fibonacci numbers
public function sum_fibonacci_number($n)
{
if ($n <= 0)
{
return;
}
else
{
//create auxiliary space
$auxiliary = array_fill(0, $n, 0);
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
//Set initial sequence
$auxiliary[0] = 0;
$auxiliary[1] = 1;
//resultant variable
$sum = 0;
//Calculate the sum of n fibonacci numbers
for ($i = 0; $i < $n; ++$i)
{
if ($i > 1)
{
$auxiliary[$i] = $auxiliary[$i - 2] + $auxiliary[$i - 1];
}
// Add fibonacci number
$sum += $auxiliary[$i];
}
echo "\n Sum of ". $n ." fibonacci number is ". $sum;
}
}
}
function main()
{
$obj = new MyMaths();
//Test case
$obj->sum_fibonacci_number(7);
$obj->sum_fibonacci_number(10);
}
main();
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
// Node Js Program
// Sum of n fibonacci numbers
class MyMaths
{
//Calculating the sum of n fibonacci numbers
sum_fibonacci_number(n)
{
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
var auxiliary = Array(n).fill(0);
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
//Set initial sequence
auxiliary[0] = 0;
auxiliary[1] = 1;
//resultant variable
var sum = 0;
//Calculate the sum of n fibonacci numbers
for (var i = 0; i < n; ++i)
{
if (i > 1)
{
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1];
}
// Add fibonacci number
sum += auxiliary[i];
}
process.stdout.write("\n Sum of " + n + " fibonacci number is " + sum);
}
}
}
function main()
{
var obj = new MyMaths();
//Test case
obj.sum_fibonacci_number(7);
obj.sum_fibonacci_number(10);
}
main();
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
# Python 3 Program
# Sum of n fibonacci numbers
class MyMaths :
# Calculating the sum of n fibonacci numbers
def sum_fibonacci_number(self, n) :
if (n <= 0) :
return
else :
# create auxiliary space
auxiliary = [0] * n
# Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
# Set initial sequence
auxiliary[0] = 0
auxiliary[1] = 1
# loop controlling variable
i = 0
# resultant variable
sum = 0
# Calculate the sum of n fibonacci numbers
while (i < n) :
if (i > 1) :
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1]
# Add fibonacci number
sum += auxiliary[i]
i += 1
print("\n Sum of ", n ," fibonacci number is ", sum, end = "")
def main() :
obj = MyMaths()
# Test case
obj.sum_fibonacci_number(7)
obj.sum_fibonacci_number(10)
if __name__ == "__main__": main()
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
# Ruby Program
# Sum of n fibonacci numbers
class MyMaths
# Calculating the sum of n fibonacci numbers
def sum_fibonacci_number(n)
if (n <= 0)
return
else
# create auxiliary space
auxiliary = Array.new(n) {0}
# Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
# Set initial sequence
auxiliary[0] = 0
auxiliary[1] = 1
# loop controlling variable
i = 0
# resultant variable
sum = 0
# Calculate the sum of n fibonacci numbers
while (i < n)
if (i > 1)
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1]
end
# Add fibonacci number
sum += auxiliary[i]
i += 1
end
print("\n Sum of ", n ," fibonacci number is ", sum)
end
end
end
def main()
obj = MyMaths.new()
# Test case
obj.sum_fibonacci_number(7)
obj.sum_fibonacci_number(10)
end
main()
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
// Scala Program
// Sum of n fibonacci numbers
class MyMaths
{
//Calculating the sum of n fibonacci numbers
def sum_fibonacci_number(n: Int): Unit = {
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
var auxiliary: Array[Int] = Array.fill[Int](n)(0);
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
//Set initial sequence
auxiliary(0) = 0;
auxiliary(1) = 1;
//loop controlling variable
var i: Int = 0;
//resultant variable
var sum: Long = 0;
//Calculate the sum of n fibonacci numbers
while (i < n)
{
if (i > 1)
{
auxiliary(i) = auxiliary(i - 2) + auxiliary(i - 1);
}
// Add fibonacci number
sum += auxiliary(i);
i += 1;
}
print("\n Sum of " + n + " fibonacci number is " + sum);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMaths = new MyMaths();
//Test case
obj.sum_fibonacci_number(7);
obj.sum_fibonacci_number(10);
}
}
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

```
// Swift Program
// Sum of n fibonacci numbers
class MyMaths
{
//Calculating the sum of n fibonacci numbers
func sum_fibonacci_number(_ n: Int)
{
if (n <= 0)
{
return;
}
else
{
//create auxiliary space
var auxiliary: [Int] = Array(repeating: 0, count: n);
//Sequence = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...
//Set initial sequence
auxiliary[0] = 0;
auxiliary[1] = 1;
//loop controlling variable
var i: Int = 0;
//resultant variable
var sum: Int = 0;
//Calculate the sum of n fibonacci numbers
while (i < n)
{
if (i > 1)
{
auxiliary[i] = auxiliary[i - 2] + auxiliary[i - 1];
}
// Add fibonacci number
sum += auxiliary[i];
i += 1;
}
print("\n Sum of ", n ," fibonacci number is ", sum, terminator: "");
}
}
}
func main()
{
let obj: MyMaths = MyMaths();
//Test case
obj.sum_fibonacci_number(7);
obj.sum_fibonacci_number(10);
}
main();
```

#### Output

```
Sum of 7 fibonacci number is 20
Sum of 10 fibonacci number is 88
```

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

## New Comment