Advertisement
dimipan80

6.20Loops_CalculateNFibonacciNumber

Mar 23rd, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class FibonacciNumbers
  5. {
  6.     static void Main ()
  7.     {
  8.         Console.Write("Please, enter a whole positive number for N = ");
  9.         string numberStr = Console.ReadLine();
  10.         int numN = int.Parse(numberStr);
  11.         if (numN < 1 || numN > 100000)
  12.         {
  13.             Console.WriteLine("Error - The number {0} is out of range !!!");
  14.         }
  15.         else
  16.         {
  17.             checked
  18.             {
  19.                 BigInteger fibonacciFirst = 1;
  20.                 BigInteger fibonacciSecond = 1;
  21.                 Console.WriteLine("The First N members of Fibonacci Numbers are:");
  22.                 if (numN == 1)
  23.                 {
  24.                     Console.WriteLine("{0}, ", fibonacciFirst);
  25.                 }
  26.                 else
  27.                 {
  28.                     Console.Write("{0}, {1}, ", fibonacciFirst, fibonacciSecond);
  29.  
  30.                     BigInteger fibonacciN = 1;
  31.                     for (int i = 3; i <= numN; i++)
  32.                     {
  33.                         fibonacciN = fibonacciFirst + fibonacciSecond;
  34.                         Console.Write("{0}, ", fibonacciN);
  35.                         fibonacciFirst = fibonacciSecond;
  36.                         fibonacciSecond = fibonacciN;                        
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.         Console.ReadLine();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement