Advertisement
braveheart1989

Fibonacci Numbers

Feb 16th, 2016
91
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.Text;
  3.  
  4. class SquareofStars
  5. {
  6.     static void Main(string[] args)
  7.     {    
  8.         //Problem 10.* Fibonacci Numbers
  9.  
  10.         int n = int.Parse(Console.ReadLine());
  11.         if (n == 0)
  12.         {
  13.             Console.WriteLine("Invalid input");
  14.         }
  15.         else if (n == 1)
  16.         {
  17.             Console.WriteLine(0);
  18.             return;
  19.         }
  20.  
  21.         int x1 = 0;
  22.         int x2 = 1;
  23.         Console.Write("{0} {1} ", x1, x2);
  24.         int x3;
  25.  
  26.         for (int i = 2; i <= n; i++)
  27.         {
  28.  
  29.             x3 = x1 + x2;
  30.             if (i!=n)
  31.             {
  32.                 Console.Write(" {0},", x3);
  33.  
  34.             }
  35.             else if (i==n)
  36.             {
  37.                 Console.Write(" {0}", x3);
  38.  
  39.             }
  40.  
  41.             x1 = x2;
  42.             x2 = x3;
  43.         }
  44.  
  45.         Console.WriteLine();
  46.  
  47.  
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement