In this Python examples tutorial, you will learn how to print natural numbers in Python. In this guide, we will see various ways to write a python program to print naturals numbers.
If you are a beginner python programmer, then this example will very helpful for you.
Before writing a python program to print natural numbers, we will see what actually is a natural number?
Headings of Contents
What is natural number?
A natural number is an integer greater than 0. Natural numbers begin at 1 and increment to infinity. For example: 1, 2, 3, 4, 5, etc.
Python program to print natural numbers
In this example, we will see several ways to print natural numbers.
Python program to print natural numbers using for loop:
In this example, we will print natural numbers up to n terms. Basically, we will ask numbers from the user to print natural numbers up to that number. Here we have used python for loop to print natural numbers.
#ask number from the user
n = int(input("Enter any positive number:- "))
print("Natural number from 1 to {0} are:-".format(n))
#print natural number using for loop.
for i in range(1, n + 1):
print(i, end=" ")
Output
Enter any positive number:- 12
Natural number from 1 to 12 are:- 1 2 3 4 5 6 7 8 9 10 11 12
Python program to print natural numbers using function:
In this example, we will use the python function to print the natural numbers up to n terms.
#ask number from the user
num = int(input("Enter any positive number:- "))
print("Natural number from 1 to {0} are:-".format(num))
#Custom function to print natural numbers
def naturalNumber(n):
#print natural number using for loop.
for i in range(1, n + 1):
print(i, end=" ")
# Call the function
naturalNumber(num)
Output
Natural number from 1 to 20 are:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Python program to print natural numbers using while loop:
Here we have used python while loop to print natural numbers upto n terms.
#ask number from the user
n = int(input("Enter any positive number:- "))
print("Natural number from 1 to {0} are:-".format(n))
i = 1
#While loop to print natural numbers
while (i <= n ):
print(i, end=" ")
i = i + 1
Output
Enter any positive number:- 10
Natural number from 1 to 10 are:-
1 2 3 4 5 6 7 8 9 10
Conclusion
In this tutorial, you have learned how to write a python program to print natural numbers.
Here have seen various ways to print naturals numbers. This is the very basic python program for you If you are a beginner python programmer. Keep visiting here for further easy and complex python programs.
Other Python examples
- Python program to find the smallest number in a list
- Python program to reverse a list
- Python program to reverse a list ( 5 Ways )
For more information:- Click Here