Calculate the sum of the integers from 1 to 10 using while loop - CSharp Language Basics

CSharp examples for Language Basics:while

Description

Calculate the sum of the integers from 1 to 10 using while loop

Demo Code

using System;/*from  ww w .j  a  va2 s.c  om*/
class Calculate
{
   static void Main()
   {
      int sum = 0; // initialize sum to 0 for totaling
      int x = 1; // initialize x to 1 for counting
      while (x <= 10) // while x is less than or equal to 10
      {
         sum += x; // add x to sum
         ++x; // increment x
      }
      Console.WriteLine($"The sum is: {sum}");
   }
}

Result


Related Tutorials