160k views
2 votes
Write a C program that outputs all integers within a range. The program should take 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 and 10, the output is:

User Kidus
by
7.0k points

1 Answer

3 votes

Final answer:

The question is asking for a C program that prints all integers in a given range, depending on whether the range is inclusive or exclusive. The program will use a for loop to iterate from the low to the high value and print each integer.

Step-by-step explanation:

To write a C program that outputs all integers within a specified range, we can use a for loop to iterate through the numbers. Depending on whether the range is inclusive or exclusive, we will adjust the starting and ending points of our loop. Here's an example of how you might structure such a program: #include int main() { int low, high, inclusive; printf("Enter the lower and higher range values, and 0 for exclusive or 1 for inclusive: "); scanf("%d %d %d", &low, &high, &inclusive); if (inclusive) { for (int i = low; i <= high; i++) { printf("%d\\", i); } else {for (int i = low + 1; i < high; i++) {printf("%d\\", i); } } return 0;}

This program first prompts the user to enter the low and high range values and whether the range should be inclusive or exclusive. It then uses the for loop to iterate through the numbers within the specified range and prints each one out.

User Dpbataller
by
7.3k points