Mega Code Archive

 
Categories / C# / Language Basics
 

While loop to calculate and display the Fibonacci numbers less than 50

/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /*   Example4_9.cs illustrates the use of   a while loop to calculate and display   the Fibonacci numbers less than 50 */ public class Example4_9 {   public static void Main()   {     // initialize the first two numbers in the sequence     int oldNumber = 1;     int currentNumber = 1;     int nextNumber;     System.Console.Write(currentNumber + " ");     while (currentNumber < 50)     {       System.Console.Write(currentNumber + " ");       // calculate the next number by adding the       // current number to the old number       nextNumber = currentNumber + oldNumber;       oldNumber = currentNumber;       currentNumber = nextNumber;     }   } }