jloga4
12/30/2016 - 3:07 PM

Using a while loop to find the sum of the numbers from 1 to n. From Fundamentals of Computer Programming with CSharp - http://www.introprogr

Using a while loop to find the sum of the numbers from 1 to n. From Fundamentals of Computer Programming with CSharp - http://www.introprogramming.info/wp-content/uploads/2013/07/Books/CSharpEn/Fundamentals-of-Computer-Programming-with-CSharp-Nakov-eBook-v2013.pdf

Console.Write("n = ");
int n = int.Parse(Console.ReadLine());
int num = 1;
int sum = 1;
Console.Write("The sum 1");

while (num < n)
{
  num++;
  sum += num;
  Console.Write(" + " + num);
}

Console.WriteLine( " = " + sum);

// n = 6
// The sum 1 + 2 + 3 + 4 + 5 + 6 = 21