Author : Rahul

Write a program to print the Fibonacci Series


Fibonacci Series: 

A number series is said to be Fibonacci series, if the sum of last two number gives the value of next number. For Example 0,1,1,2,3,5 is a Fibonacci series because here the sum of 0 and 1 is 1 which is the next number. Again sum of 1 and 1 is 2 which is the next number and so on.

Program:

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string numOfTermsVal;
     int numOfTerms, firstNum, secondNum, thirdNum;
     
     //User Input
     Console.Write("Enter Number of Items in Series:");
     numOfTermsVal = Console.ReadLine();
     numOfTerms = Convert.ToInt32(numOfTermsVal);
     
     //Fibonacci Series Logic and Print Result
     firstNum = 0;
     secondNum = 1;
     Console.WriteLine($"Fibonacci Series:");
     Console.Write(firstNum + " , " + secondNum);
     for (int i = 1; i <= numOfTerms - 2; i++)
     {
       thirdNum = firstNum + secondNum;
       Console.Write(" , " + thirdNum);
       secondNum = thirdNum;
       firstNum = secondNum;
     }
     Console.Read();
   }
 }
}

Output:

Enter Number of Items in Series:10
Fibonacci Series:
0 , 1 , 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128