紧求!!!几道基础的c语言题目

来源:百度知道 编辑:UC知道 时间:2024/05/21 07:07:32
1.编写程序absolute.c ,实现对x求绝对值。
2.编写程序sum.c,求以下表达式结果并输出:1+3+5+7+9+11

1、
int x;
if(x<0)
x=-x;

2、
int s;
s=1+3+5+7+9+11;
print(%d,s);

1.1
#include <stdio.h>
#include <math.h>
main()
{
int x;
scanf("%d",&x);
if(x!=0)
{
x=abs(x);
printf("abs(x)=%d\n",x);
else printf("%d",0)
}

1.2
#include <stdio.h>
main()
{
int x;
scanf("%d",&x);
if(x<=0)
{
x=-x;
printf("%d\n",x);
}
else printf("%d\n",x);
}

2.1
#include <stdio.h>
main()
{
int sum=0,i=1;
if(i<=11)
{
sum=sum+i;
i=i+2;
}
printf("%d\n",sum);
}