To sum all numbers up to 10 in C#, we can use a for
loop and an accumulator variable to keep track of the sum. Here's an example code block that accomplishes this:
main.cs124 chars6 lines
In this code, we first initialize our accumulator variable sum
to 0. We then use a for
loop to iterate over the numbers from 1 to 10. In each iteration of the loop, we add the current value of i
to the sum
variable. Finally, we print out the value of sum
using Console.WriteLine()
.
This code should output: The sum of all numbers up to 10 is: 55
gistlibby LogSnag