Advertisement
VyaraG

Fibonacci

Nov 29th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System;
  2.  
  3. //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) : 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.
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         int lengthFi = int.Parse(Console.ReadLine());
  10.         int tempA = 0;
  11.         int tempB = 1;
  12.  
  13.  
  14.         for (int i = 0; i < lengthFi; i++)
  15.         {
  16.            Console.Write(tempA + " ");
  17.            int temp = tempA;
  18.            tempA = tempB;
  19.            tempB = temp + tempB;
  20.  
  21.         }
  22.         Console.WriteLine();        
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement