-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1101.c
More file actions
35 lines (30 loc) · 929 Bytes
/
problem1101.c
File metadata and controls
35 lines (30 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Read an undetermined number of pairs values M and N
(stop when any of these values is less or equal to zero).
For each pair, print the sequence from the smallest to the biggest
(including both) and the sum of consecutive integers between them
(including both).*/
#include <stdio.h>
int main() {
int M, N, i;
// Loop until either M or N is less than or equal to zero
while (1) {
scanf("%d%d", &M, &N);
if (M <= 0 || N <= 0) {
break;
}
int sum = 0;
if (M > N) {
// Swap M and N to ensure M is always less than or equal to N
int temp = M;
M = N;
N = temp;
}
// Print the sequence and calculate the sum
for (i = M; i <= N; i++) {
printf("%d ", i);
sum += i;
}
printf("Sum=%d\n", sum);
}
return 0;
}