Topic: Function
Write a function-‐oriented program that generates the Fibonacci series numbers of n (as input) and display them. In Fibonacci, the current third number is the sum of two previous
numbers.
Note: you can choose any looping statement if need.
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
class Program
{
static void Fibonacci(int n)
{
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]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
Console.Write("Enter n: ");
int n = int.Parse(Console.ReadLine());
Fibonacci(n);
Console.ReadLine();
}
}
}
Comments
Leave a comment