问题:
Write a program that
(1) inputs two integers (integer1 and integer 2)
(2) prints sum of all integers between integer1 and integer2
(3) Use while() statement
Ex) input : 10, 15 -> Sum : 10 + 11+ 12+ 13 +14 + 15 = 75
#include<stdio.h>
#include<stdlib.h>
int main(void) {
int num1, num2;
int sum = 0;
int start, end;
printf("Enter two numbers:");
scanf_s("%d %d", &num1, &num2);
if (num1 < num2) {
start = num1;
end = num2;
}
else {
start = num2;
end = num1;
}
while (start <= end) {
sum = sum + start;
start = start + 1;
}
printf("The sum of all integers between %d and %d is %d\n", num1, num2, sum);
system("pause");
return 0;
}