c语言编程。输入两个正整数,分别赋给变量a,b,计算ab之间所有整数之和。

2024-10-27 17:14:21
推荐回答(3个)
回答1:

#include
void main()
{
int a,b,sum=0;
printf("请输入两个正整数:\n");
scanf("%d %d",&a,&b);
if(a>b)
{
int temp;
temp=a;a=b;b=temp;
}
for(int i=a;i<=b;i++)sum+=i;
printf("两者之间所有正整数的和为:%d\n",sum);
}

回答2:

int main()
{
int a;
int b;

printf("%d\n",a==b?0:(a>b?(a-b-1):(b-a-1)));
return 0;
}

回答3:

#include
int main()
{
int a, b;
int sum;
int temp;
printf("input two integrated number:\n");
scanf("%d%d",&a, &b);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
sum = (a + b)*(a - b + 1) / 2;
printf("sum:%d\n",sum);
return 0;
}