157k views
0 votes
Write a program that outputs all integers within a range. The program takes three integer inputs. The first two integers are the low and high integers of the range. The third integer indicates whether the range is inclusive or exclusive: 0 excludes the low and high integers, and 1 includes the low and high integers. Assume the first two integers are never the same. You are required to use a for loop. For example, if the input is 5 10 1, the output is 5 6 7 8 9 10. If the input is 5 10 0, the output is 6 7 8 9. If the input is 6 7 0, there is no output. For coding simplicity, output a space after every integer, including the last.

1 Answer

5 votes

Final answer:

The question involves writing a program that outputs all integers within a range defined by two input integers and a third integer that specifies inclusivity. A for loop is used to print the range, with slight modifications to start and end depending on whether the range is inclusive or exclusive.

Step-by-step explanation:

Program to Output Integers Within a Range

To write a program that outputs all integers within a given range, including or excluding the boundaries based on the input, a simple for loop can be used. The loop will iterate through the numbers starting from the lower bound and ending at the upper bound. The third input parameter will determine whether the endpoints are inclusive (1) or exclusive (0).

Python Example Code

Here is an example code snippet in Python:

low, high, inclusive = map(int, input("Enter low high inclusive: ").split())

if inclusive:
start = low
end = high + 1 # To include the high value
else:
start = low + 1 # To exclude the low value
end = high

for i in range(start, end):
print(i, end=' ')

This code will output all the integers within the specified range according to the inclusive/exclusive input. For instance, if the input is '5 10 1', the output will be '5 6 7 8 9 10'.

User Repoman
by
7.8k points