Home » Python programs

Python | Program to print numbers from N to 1 (use range() with reverse order)



Here, we will learn how to print numbers in reverse order i.e. how to use range() method in reverse order/ decreasing steps.
Submitted by IncludeHelp, on July 29, 2018

Given the value of N and we have to print numbers from N to 1 in Python.

range() Method

This method is used to iterate a range values.

Simply, we use range(start, stop)

Let’s understand by an example, if we want to iterate any loop till a to b, then range statement will be range(a, b+1).

Iterate in reverse order

To iterate range in reverse order, we use 3 parameters

  1. Start – start value
  2. Stop – end value
  3. Step – Increment/Decrement to the value

Examples:

1) To print numbers from B to A

for i in range(B, A-1, -1)
	print i

2) To print numbers from B to A by escaping one number between

for i in range(B, A-1, -2)
	print i

Program to print numbers from N to 1 in Python

# Python program to print numbers
# from n to 1

# input the value of n
n = int(input("Enter the value of n: "))

# check the input value
if (n<=1):
	print "n should be greater than 1"
	exit()
	
# print the value of n
print "value of n: ",n

# print the numbers from n to 1
# message
print "numbers from {0} to {1} are: ".format(n,1)

# loop to print numbers 
for i in range(n,0,-1):
	print i

Output 1

    Enter the value of n: 10
    value of n:  10
    numbers from 10 to 1 are: 
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1

Output 2 (when the value of n is 1)

    Enter the value of n: 1
    n should be greater than 1





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Quick links
Latest articles, Internship, Members
New...
Coding problems, Algorithms, Discrete Mathematics, Big data
Languages
C, C++, C++ STL, Java, Data Structure, C#.Net, Android, Kotlin, SQL
Web
PHP, Python, JavaScript, CSS, Ajax, Node.js, Web prog.
Programs
C, C++, DS, Java, C#, Python
Aptitude
C, C++, Java, DBMS
Interview
C, Embedded C, Java, SEO, HR
CS Subjects
CS Basics O.S. Networks DBMS Embedded Systems Cloud Computing Machine learning CS Organizations Linux DOS
More...
Articles, Puzzles, News/Updates


Recommended posts
C Tips & Tricks, C++ Tips & Tricks
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distros of 2018
C programming optimization techniques
Differences b/w C & Embedded C?
Embedded C Interview Q. & A.
C programming tips for Embedded Development
Basic rules of writing a C program
Important points (rules) to remember while writing C/C++ program
Top 5 Websites for solving programming challenges
Read more...


Others...
Computer G.K. (MCQ)
Most viewed pages...
Categories...



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.