Use a while loop to calculate and display the Fibonacci numbers less than 50 : While « Statement « C# / CSharp Tutorial






class MainClass
{

  public static void Main()
  {
    int oldNumber = 1;
    int currentNumber = 1;

    int nextNumber;

    System.Console.Write(currentNumber + " ");

    while (currentNumber < 50)
    {
      System.Console.Write(currentNumber + " ");

      nextNumber = currentNumber + oldNumber;

      oldNumber = currentNumber;
      currentNumber = nextNumber;
    }
  }
}
1 1 2 3 5 8 13 21 34








4.5.While
4.5.1.Simplest While loop
4.5.2.Use a while loop to display 1 to 5
4.5.3.Use count number to control while loop
4.5.4.Compute integer powers of 2 with while loop
4.5.5.Use a while loop to calculate and display the Fibonacci numbers less than 50