Series – 2. Given two integers A and B, print all numbers from A to B inclusively in ascending order if A is less than B, otherwise print them in descending order.
|
A = int(input()) B = int(input()) if A <= B: for number in range(A, B + 1): print(number) else: for number in range(A, B - 1, -1): print(number) |
- Reading Input:
A = int(input())
B = int(input())
- The
input()
function reads a line of text from the user.
- The
int()
function converts the input text to an integer.
A
and B
store the integer values provided by the user.
- Conditional Statement:
- This condition checks if
A
is less than or equal to B
.
- Ascending Order:
for number in range(A, B + 1):
print(number)
- If
A
is less than or equal to B
, a for
loop iterates over the range from A
to B
(inclusive).
- The
print()
function outputs each number in this range.
- Descending Order:
else:
for number in range(A, B - 1, -1):
print(number)
- If
A
is greater than B
, a for
loop iterates over the range from A
to B
(inclusive) in reverse order.
- The
range(A, B - 1, -1)
function generates a sequence of numbers from A
to B
in steps of -1
.