Python Practice › Loop Programs › Program 11
Natural numbers 1 to n with while
Write a Python program to print the natural numbers from 1 to n using a while loop.
Input
n = 10
Output
1 2 3 4 5 6 7 8 9 10
Approach
- Start a counter at 1 and increase it until it passes n.
Solution
n = 10i = 1while i <= n: print(i, end=" ") i += 1print()Output
1 2 3 4 5 6 7 8 9 10
Line by line
Line 5
Forgetting this line would make the loop run forever.
Learn the concept
New to this topic? Read the lesson, then come back.