BonchoBelutov

Fibbonacci Sequence

Apr 13th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FibonacciNumbers
  8. {
  9.     class FibonacciNumbers
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13. //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 comma and space - ", ") : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ….
  14. //On the only line you should print the first N numbers of the sequence, separated by ", " (comma and space)
  15. //1 <= N <= 50   N will always be a valid positive integer number
  16.  
  17.             int n = int.Parse(Console.ReadLine());
  18.             int firstNum = 0;
  19.             int secondNum = 1;
  20.             int thirdNum = 0;
  21.             if (n>=1&&n<=50)
  22.             {
  23.  
  24.                 if (n == 1)
  25.                 {
  26.                     Console.WriteLine(firstNum);
  27.                 }
  28.                 else if (n == 2)
  29.                 {
  30.                     Console.WriteLine("{0}, {1}", firstNum, secondNum);
  31.                 }
  32.                 else if (n > 2)
  33.                 {
  34.                     Console.Write("{0}, {1}, ", firstNum, secondNum);
  35.                     for (int i = 1; i <= n - 2; i++)
  36.                     {
  37.                         thirdNum = firstNum + secondNum;
  38.                         firstNum = secondNum;
  39.                         secondNum = thirdNum;
  40.                         if (n - 2 == i)
  41.                         {
  42.                             Console.WriteLine(thirdNum);
  43.                         }
  44.                         else
  45.                         {
  46.                             Console.Write(thirdNum + ", ");
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment