“Write an application named sum500 that sums the integers from 1 to 500.” (Joyce Farrel, Microsoft Visual c# 2015: an introduction to object-oriented programming)
this pe.9 from chapter 5. We aren’t expted to use anything fancy, maybe just a for loop or nested loops that’s about it. Thank you. Please use c#.
static void Main(string args)
int i, sum = 0,n=500;
for (i = 1; i <= n; i++)
sum = sum + i;
Console.WriteLine(“nSum of 500 Numbers : ” + sum);
The above C# program is the simplest way to add integers from 1 to 500. The key thing is loop.
The structure of program is same as any other C# program .
First include System by using keyword
Then declare a class named sum500 . Your code goes inside the main function. The program starts execution from here.
declare a variable i whose value will vary from 1 to 500 in the loop. Inititalise a variable sum as 0. It will store the sum of numbers
start the loop from i=1 till i=500
Value of i is added to sum each time the loop executes
So in the end we get sum=1+2+3+4……..500
Then print the sum . AS SIMPLE AS THAT. hope it helped you.