76.2k views
4 votes
10. write a c program that inputs two integers and outputs the sum of the integers between them. sample input 9 12 sample output the sum of the numbers between 9 and 12 inclusive is 42.

1 Answer

5 votes

Final answer:

A C program is provided that calculates the sum of all integers between two given numbers, using a for loop and basic input/output functions.

Step-by-step explanation:

The C program required will calculate the sum of all integers between two given numbers, including the numbers themselves. This task involves using a simple for loop to iterate through the numbers and accumulate their sum. Here is an example of how the C program could look:

#include
int main() {
int start, end, sum = 0;
printf("Enter the two integers: ");
scanf("%d %d", &start, &end);
for(int i = start; i <= end; i++) {
sum += i;
}
printf("The sum of the numbers between %d and %d inclusive is %d.\\", start, end, sum);
return 0;
}

Note that the program assumes the first number entered will be less than or equal to the second number. If this might not always be the case, you could add a check to ensure that start is less than end and swap their values if it's not.

User TreeAndLeaf
by
8.6k points