Create a program generate the Fibonacci Sequence and compute for the
corresponding sum based on the generated sequences.
Sample output:
Enter how many: 5
Fibonacci Sequence: 1 1 2 3 5
Sum of Sequence: 12
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter how many: ");
int n = int.Parse(Console.ReadLine());
int sum = 0;
int[] fibonacciSequence = new int[n];
fibonacciSequence[0] = 1;
fibonacciSequence[1] = 1;
for (int i = 2; i < fibonacciSequence.Length; i++)
{
fibonacciSequence[i] = fibonacciSequence[i - 1] + fibonacciSequence[i - 2];
}
Console.Write("Fibonacci Sequence: ");
for (int i = 0; i < fibonacciSequence.Length; i++)
{
Console.Write("{0} ", fibonacciSequence[i]);
sum += fibonacciSequence[i];
}
Console.WriteLine();
Console.WriteLine("Sum of Sequence: {0}", sum);
Console.ReadLine();
}
}
}
Comments
Leave a comment