Write a python program to print all natural numbers (from 1 to n). - using while loop

Write a python program to print all natural numbers (from 1 to n). - using while loop

Python program to print numbers from n to 1 and 1 to n; In this tutorial, you will learn how to print numbers from n to 1 and n to 1 using for loop and while loop.

  • Python program to print numbers from 1 to N using for loop
  • Python program to print numbers from N to 1 using while loop

Python program to print numbers from 1 to N using for loop

  • Take the input from the user by using python input() function.
  • Iterate for loop with the user input number.
  • Increment for loop iteration value by 1, as well as print iteration value.

# Python program to print numbers from 1 to n

n = int(input("Please Enter any Number: "))

print("The List of Natural Numbers from 1", "to", n) 

for i in range(1, n + 1):
    print (i, end = '  ')

Output

Please Enter any Number:  15 
The List of Natural Numbers from 1 to 15 
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  

Python program to print numbers from n to 1 using while loop

  • Take the input from the user by using python input() function.
  • Iterate while loop with the user input number.
  • Decrement while loop iteration value by 1, as well as print iteration value.

# Python program to print numbers from n to 1

number = int(input("Please Enter any Number: "))
i = number

while ( i >= 1):
    print (i, end = '  ')
    i = i - 1

Output

Please Enter any Number:  5 
5  4  3  2  1

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

View all posts by Admin

Post navigation

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:

  1. Using for loop
  2. Using while loop
  3. Using functions

So, without further ado, let’s begin this tutorial.

  • Python Program to Print Natural Numbers From 1 to N
  • How Does This Program Work ?
  • Python Program to Print Natural Number Using While Loop
  • Python Program to Print Natural Numbers Using Functions
  • Conclusion

# 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:

  • Python Program to Reverse a String
  • Python Program to Display Calendar
  • Python Program to Check Leap Year
  • Python Program to Find Largest of 3 Numbers
  • Python Program to Reverse a String Using Recursion

How do you print n natural numbers in Python while loop?

Python program to print numbers from n to 1 using while loop Take the input from the user by using python input() function. Iterate while loop with the user input number. Decrement while loop iteration value by 1, as well as print iteration value.

How do you run a loop from 1 to n in Python?

“for i to n python” Code Answer's.
for i in range (m,n+1):.
s += i..
print(i).
print(s).

How do you print a number from 1 to 10 while loop?

To print the numbers from 1 to 10, We will declare a variable for loop counter (number). We will check the condition whether loop counter is less than or equal to 10, if condition is true numbers will be printed. If condition is false – loop will be terminated.