Advertisement
pupesko

SoftUni - 4.10* Fibonacci Numbers

Oct 2nd, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. //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.
  2.  
  3. using System;
  4.  
  5. class FibonachiNums
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Fibonachi numbers to display: ");
  10.         string nums = Console.ReadLine();
  11.         int totalNums = int.Parse(nums);
  12.  
  13.         ulong result = 0;
  14.         ulong a = 0;
  15.         ulong b = 1;
  16.  
  17.         Console.Write("Fibonachi sequence - ");
  18.  
  19.         for (int i = 0; i < totalNums; i++)
  20.         {
  21.             Console.Write(" {0}", result);
  22.  
  23.             a = result + b;
  24.             result = b;
  25.             b = a;
  26.         }
  27.         Console.WriteLine();
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement