Advertisement
Stann

07.FibonacciSum

Nov 28th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class SequenceOfFibonacci
  5. {
  6. static void Main()
  7. {
  8. /* Write a program that reads a number N and calculates the sum of the first N members of the sequence of Fibonacci:
  9. * 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, … */
  10.  
  11. Console.WriteLine("Enter number of members that you want to sum");
  12. int n = int.Parse(Console.ReadLine());
  13. BigInteger numOne = 0;
  14. BigInteger numTwo = 1;
  15. BigInteger nextNum;
  16. BigInteger sum = 0;
  17. for (int i = 0; i <= n-2;i++)
  18. {
  19. nextNum = numOne + numTwo;
  20. numOne = numTwo;
  21. numTwo = nextNum;
  22. sum += numOne;
  23. }
  24. Console.WriteLine("The sum of first {0} members of sequence of Fibonacci is {1}", n, sum);
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement