bhalash

Project Euler problem #2 solution.

May 29th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.57 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class problem
  5. {
  6.     static void Main()
  7.     {
  8.         Problem();
  9.         Solution();
  10.     }
  11.  
  12.     static void Problem()
  13.     {
  14.         Console.WriteLine("\nBy considering the terms in the Fibonacci sequence whose values do not exceed\nfour million, find the sum of the even-valued terms:");
  15.     }
  16.  
  17.     static void Solution()
  18.     {
  19.         int a   = 1;
  20.         int b   = 1;
  21.         int c   = 0;
  22.  
  23.         int sum = 0;
  24.         int cap = 4000000;
  25.  
  26.         do
  27.         {
  28.             c = a + b;
  29.  
  30.             if (c % 2 == 0)
  31.                 sum += c;
  32.  
  33.             a = b;
  34.             b = c;
  35.  
  36.         } while (c <= cap);
  37.  
  38.         Console.WriteLine("\n{0}\n",sum);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment