Venciity

[ Telerik C#] LOOPS - EX 7

Feb 9th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. /* Write a program that reads a number N and calculates the sum of the first N members
  2.  * of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
  3.  * Each member of the Fibonacci sequence (except the first two) is a sum of the previous two members.
  4.  */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ex7
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.Write("Enter N: ");
  18.             ulong n = ulong.Parse(Console.ReadLine());
  19.  
  20.             ulong firstN = 1;
  21.             ulong secondN = 0;
  22.             ulong thirdN = 0;
  23.             ulong sum = 0;
  24.  
  25.             for (ulong i = 0; i <= n; i++)
  26.             {
  27.                 thirdN = firstN + secondN;
  28.                 firstN = secondN;
  29.                 secondN = thirdN;
  30.                 Console.WriteLine("{0} : = {1}", i, thirdN);
  31.                 sum += thirdN;
  32.             }
  33.             Console.WriteLine("Sum is : {0}" ,sum);
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment