Summing integers with the for statement. - CSharp Language Basics

CSharp examples for Language Basics:for

Description

Summing integers with the for statement.

Demo Code

using System;/*from   ww  w .j a  va2 s.c  o m*/
class Sum
{
   static void Main()
   {
      int total = 0; // initialize total
      // total even integers from 2 through 20
      for (int number = 2; number <= 20; number += 2)
      {
         total += number;
      }
      Console.WriteLine($"Sum is {total}"); // display results
   }
}

Result


Related Tutorials