To print numbers in C#, you can use a for loop or a while loop. Here is an example of how to print numbers from 1 to 10 using a for loop in C#:
main.cs60 chars5 lines
This code declares a variable i
and initializes it to 1. It then performs an iteration while i
is less than or equal to 10. In each iteration, it prints the value of i
to the console using Console.WriteLine()
, and then increments the value of i
by 1 with i++
.
You can use a similar loop to print numbers with different starting and ending values, or with a different increment. For example, to print even numbers from 2 to 20, you could use:
main.cs63 chars5 lines
This example initializes i
to 2, and then increments it by 2 in each iteration with i += 2
.
gistlibby LogSnag