Python Program to Print Numbers From N to 1 and 1 to N

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 N to 1 and 1 to N

Let’s follow the following algorithm to write a python program to print numbers from 1 to N and N to 1 using while loop and for 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

Recommended Python Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *