Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that reads a number N and calculates the sum of the first N members
- * of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
- * Each member of the Fibonacci sequence (except the first two) is a sum of the previous two members.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ex7
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Enter N: ");
- ulong n = ulong.Parse(Console.ReadLine());
- ulong firstN = 1;
- ulong secondN = 0;
- ulong thirdN = 0;
- ulong sum = 0;
- for (ulong i = 0; i <= n; i++)
- {
- thirdN = firstN + secondN;
- firstN = secondN;
- secondN = thirdN;
- Console.WriteLine("{0} : = {1}", i, thirdN);
- sum += thirdN;
- }
- Console.WriteLine("Sum is : {0}" ,sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment