从键盘输入正整数n,计算并输出1到n的和,c语言谢谢

2024-11-06 06:57:50
推荐回答(5个)
回答1:

#include "stdio.h"
int main()
{
int n,sum=0,i;
printf("Please input a number:\n");
scanf("%d",&n);

for(i=1;i<=n;i++)
sum+=i;
printf("The sum is %d.\n",sum);

}

回答2:

这个很简单,不需要循环

#include "stdio.h"
int main()
{
int n,sum=0;
printf("Please input a number:\n");
scanf("%d",&n);

sum = (n *(n+1))/2;
printf("The sum is %d.\n",sum);
return 0;
}

回答3:

#include
void main()
{
int i,n,s=0;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
s+=i;
}
printf("%d",s);
}

回答4:

#iiclude
main(){
int i,n,s=0;
scanf("%d",&n);
for (i=1;i<=n;i++) s+=i;
printf("%d",s);
}

回答5:

void main()
{
int n,i;
int sum = 0;
scanf("%d", &n);
for(i = 1; i < n+1; i++)
{
sum = sum + i;
}

printf("%d\n", sum);
}