C# Program to Print Numbers 1 to n
Use a
for loop in C# like for (int i = 1; i <= n; i++) Console.WriteLine(i); to print numbers from 1 to n.Examples
Input1
Output1
Input5
Output1
2
3
4
5
Input0
Output
How to Think About It
To print numbers from 1 to n, start counting from 1 and keep increasing by 1 until you reach n. For each number, print it on a new line. If n is zero or less, print nothing.
Algorithm
1
Get the input number n.2
Start a counter i at 1.3
While i is less than or equal to n, do:4
Print the value of i.5
Increase i by 1.6
Stop when i becomes greater than n.Code
csharp
using System; class Program { static void Main() { int n = 5; // Change this value to print up to n for (int i = 1; i <= n; i++) { Console.WriteLine(i); } } }
Output
1
2
3
4
5
Dry Run
Let's trace printing numbers from 1 to 5 through the code
1
Initialize n and i
n = 5, i = 1
2
Check condition i <= n
1 <= 5 is true, so print 1
3
Increment i
i becomes 2
4
Repeat check and print
2 <= 5 true, print 2; i=3 3 <= 5 true, print 3; i=4 4 <= 5 true, print 4; i=5 5 <= 5 true, print 5; i=6
5
Stop loop
6 <= 5 false, loop ends
| i | Condition i <= n | Action |
|---|---|---|
| 1 | true | Print 1 |
| 2 | true | Print 2 |
| 3 | true | Print 3 |
| 4 | true | Print 4 |
| 5 | true | Print 5 |
| 6 | false | Stop |
Why This Works
Step 1: Start from 1
We begin counting at 1 because the task is to print numbers starting from 1.
Step 2: Use a loop to repeat
The for loop repeats printing and increasing the number until it reaches n.
Step 3: Stop after n
The loop stops when the number goes beyond n, ensuring only numbers from 1 to n are printed.
Alternative Approaches
while loop
csharp
using System; class Program { static void Main() { int n = 5; int i = 1; while (i <= n) { Console.WriteLine(i); i++; } } }
This uses a <code>while</code> loop instead of <code>for</code>, which is equally clear but separates initialization and increment.
recursion
csharp
using System; class Program { static void PrintNumbers(int i, int n) { if (i > n) return; Console.WriteLine(i); PrintNumbers(i + 1, n); } static void Main() { int n = 5; PrintNumbers(1, n); } }
This uses recursion to print numbers, which is elegant but less efficient for large n due to call stack use.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs from 1 to n, so it executes n times, making the time complexity O(n).
Space Complexity
Only a few variables are used regardless of n, so space complexity is O(1).
Which Approach is Fastest?
The for loop and while loop have similar performance; recursion is less efficient due to function call overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| for loop | O(n) | O(1) | Simple and clear counting |
| while loop | O(n) | O(1) | When loop condition is complex or variable |
| recursion | O(n) | O(n) | Elegant but not for large n due to stack use |
Use a
for loop for simple counting tasks like printing numbers from 1 to n.Starting the loop at 0 instead of 1, which prints numbers from 0 to n instead of 1 to n.