Python Program to Print 123…N
Python Program to Print 123…N
In this tutorial, we shall read an integer (N) from user and print all the number from 1 to N to the standard console output.
We shall use looping statements like For Loop and While Loop, to iterate from 1 to N.
Example 1: Print 1 to N using For Loop
In this example, we shall use Python For Loop to print all the numbers from 1 to N.
Python Program
n = int(input('Enter N : '))
for i in range(1,n+1):
print(i)
Output
Enter N : 4
1
2
3
4
Example 2: Print 1 to N using While Loop
In this example, we shall use Python While Loop to print all the numbers from 1 to N.
Python Program
n = int(input('Enter N : '))
i=1
while i<=n:
print(i)
i=i+1
Output
Enter N : 4
1
2
3
4
Summary
In this tutorial of Python Examples, we learned how to print all the numbers from 1 to N, for a given N, with the help of examples using for loop and while loop.