Python Practice › Loop Programs › Program 11

Natural numbers 1 to n with while

EasyLoop Programs · 11 of 90

Write a Python program to print the natural numbers from 1 to n using a while loop.

Advertisement
Input
n = 10
Output
1 2 3 4 5 6 7 8 9 10 

Approach

  1. Start a counter at 1 and increase it until it passes n.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment