Advertisement
nmnikolov

FibonacciNumbers

Jun 17th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. //Write a program that reads a number n and prints on the console the first n members of the Fibonacci sequence (at a single line, separated by spaces) :
  5. //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …. Note that you may need to learn how to use loops. Examples:
  6. // |  n | comments               |
  7. // |  1 | 0                      |
  8. // |  3 | 0 1 1                  |
  9. // | 10 | 0 1 1 2 3 5 8 13 21 34 |
  10.  
  11. class FibonacciNumbers
  12. {
  13.     static void Main()
  14.     {
  15.         Console.Write("Enter n: ");
  16.         int n = Math.Abs(int.Parse(Console.ReadLine()));
  17.         BigInteger x1 = 1;
  18.         BigInteger x2 = 0;
  19.         for (int i = 0; i < n; i++)
  20.         {
  21.             Console.Write("{0} ", x2);
  22.             x2 = x2 + x1;
  23.             x1 = x2 - x1;                          
  24.         }
  25.         Console.WriteLine();
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement