Python Program to Print All Natural Numbers from 1 to N in Iterative Methods

First N natural numbers

Python program to print all the natural numbers from 1 to N can be written in both iterative and recursive methods. In the iterative method, the task can be performed by using for loop and while loop. Here, all the Python programs using different loops have been covered to print the natural numbers from 1 to N (1, 2, 3, ..., N).






1. Python Program & output to Print First N Natural Numbers

Code has been copied
#**************************************************
#                alphabetacoder.com
# Python program to print all natual numbers  
# from 1 to n using for loop
#**************************************************

# take input of the number upto 
# which numbers will be printed
n=int(input("Enter the upper limit="))

# iterate from 1 to n and print the number
print("First ",n," natural numbers are : ")
for i in range(1,n+1):
    print(i, end=" ") 
    #end=" " is used to print elements in same line

Output


Case 1:

Enter the upper limit=10

First 10 natural numbers are :

1 2 3 4 5 6 7 8 9 10


Case 2:

Enter the upper limit=5

First 5 natural numbers are :

1 2 3 4 5

#**************************************************
#                alphabetacoder.com
# Python program to print all natual numbers  
# from 1 to n using while loop
#**************************************************

# take input of the number upto 
# which numbers will be printed
n=int(input("Enter the upper limit="))

# iterate from 1 to n and print the number
print("First ",n," natural numbers are : ")
i=1
while i<=n:
    print(i, end=" ")
    #end=" " is used to print elements in same line
    i=i+1
    

Output


Case 1:

Enter the upper limit=10

First 10 natural numbers are :

1 2 3 4 5 6 7 8 9 10


Case 2:

Enter the upper limit=5

First 5 natural numbers are :

1 2 3 4 5