In this post, we will learn how to print natural numbers from 1 to N using Python Programming language.
Natural numbers are a part of the number system used for counting which includes all the positive integers from 1 till infinity. For example: 1, 2, 3, 4, 5. . . so on.
We will be printing natural numbers using the following methods:
- Using for loop
- Using while loop
- Using functions
So, without further ado, let’s begin this tutorial.
Python Program to Print Natural Numbers From 1 to N
# Python Program to Print Natural Numbers From 1 to N num = int(input("Enter any number: ")) print("The list of natural numbers from 1 to {0} are: " .format(num)) for i in range(1, num + 1): print(i)
Output
Enter any number: 15
The list of natural numbers from 1 to 15 are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
How Does This Program Work ?
num = int(input("Enter any number: "))
The user is asked to enter the upper limit of the list.
for i in range(1, num + 1): print(i)
We print all the numbers lying between 1 to num (including both 1 and num) using for loop statement.
Python Program to Print Natural Number Using While Loop
# Python Program to Print Natural Number Using While Loop num = int(input("Enter maximum natural number: ")) print("The list of natural numbers from 1 to {0} are: " .format(num)) i = 1 while i <= num: print(i) i = i + 1
Output
Enter maximum natural number: 7
The list of natural numbers from 1 to 7 are:
1
2
3
4
5
6
7
Python Program to Print Natural Numbers Using Functions
# Python Program to Print Natural Numbers Using Functions def NaturalNumber(num): for i in range(1, num + 1): print(i) num = int(input("Enter the maximum natural number: ")) print("The list of natural numbers from 1 to {0} are: " .format(num)) NaturalNumber(num)
Output
Enter the maximum natural number: 5 The list of natural numbers from 1 to 5 are: 1 2 3 4 5
Conclusion
I hope after going through this post, you understand how to print natural numbers from 1 to N using Python Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.
Also Read: