c语言中do语句;输入两个整数,计算它们之间的所有整数的和
c语言中程序的循环控制,do语句输入两个整数,计算它们之间的所有整数的和。
1、do语句
#include <stdio.h>
int main(void)
{
int a, b, min, max, sum = 0;
puts("please input two integers.");
printf("a = "); scanf("%d", &a);
printf("b = "); scanf("%d", &b);
min = a > b ? b:a;
max = a > b ? a:b;
do
{
sum += min;
min++;
}
while(min <= max);
printf("sum = %d\n", sum);
return 0;
}
2、while语句
#include <stdio.h>
int main(void)
{
int a, b, min, max, sum = 0;
puts("please input two integers.");
printf("a = "); scanf("%d", &a);
printf("b = "); scanf("%d", &b);
min = a > b ? b:a;
max = a > b ? a:b;
while(min <= max)
{
sum += min;
min++;
}
printf("sum = %d\n", sum);
return 0;
}
3、for语句
#include <stdio.h>
int main(void)
{
int a, b, min, max, sum = 0;
puts("please input two integers.");
printf("a = "); scanf("%d", &a);
printf("b = "); scanf("%d", &b);
min = a > b ? b:a;
max = a > b ? a:b;
for(min; min <= max; min++)
{
sum += min;
}
printf("sum = %d\n", sum);
return 0;
}