prints the first 10 Fibonacci numbers, where each number is the sum of the previous two - CSharp Language Basics

CSharp examples for Language Basics:for

Description

prints the first 10 Fibonacci numbers, where each number is the sum of the previous two

Demo Code

using System;//from w  w w. j  a  v a 2s  . c om
class Test
{
   static void Main(){
      for (int i = 0, prevFib = 1, curFib = 1; i < 10; i++)
      {
         Console.WriteLine (prevFib);
         int newFib = prevFib + curFib;
         prevFib = curFib; curFib = newFib;
      }
   }
}

Result


Related Tutorials