c语言程序代码
1.要求在屏幕上输出下一行信息。
This is a c program.
程序:
#include
int main()
{
printf(“this is a c program.\n”);
return 0;
}
2.求两个整数之和。
程序:
#include
int main()
{
int a,b,sum;
a=122;
b=234;
sum=a+b;
printf(“sum is %d\n”,sum);
return 0;
}
3.求两个整数之间的较大者。
程序:
#include
int main()
{
int max(int x,int y);
int a,b,c;
scanf("%d,%d",&a,&b);
c=max(a,b);
printf("max=%d\n",c);
return 0;
}
int max(int x,int y)
{
int z;
if(x>y)z=x;
else z=y;
return(z);
}
4.有人用温度计测量出华氏发表示的温度(如69°F),今要求把
她转换成以摄氏法表示的温度(如20℃)。
公式:c=5(f-32)/9.
其中 f 代表华氏温度,c 代表摄氏温度。
程序:
#include
int main()
{
float f,c;
f=64.0;
c=(5.0/9)*(f-32);
printf("f=%f\nc=%f\n",f,c);
return 0;
}
5.计算存款利息。有1000 元,想存一年。有一下三种方法可选: (1)活期:年利率
为r1;( 2)一年定期:年利率为r2;(3) 存两次半年定期:年利率为r3。分别计算一年后按三种方法所得到的本息和。
程序:
#include
int main()
{
float
p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;
p1=p0*(1+r1);
p2=p0*(1+r2);
p3=p0*(1+r3/2)*(1+r3/2);
printf("p1=%f\np2=%f\np3=%f\n",p1,p2,p3);
return 0;
}
6.给定一个大写字母,要求以小写字母输出。
程序:
#include
int main()
{
char c1,c2;
c1='A';
c2=c1+32;
printf(“%c\n”,c2);
printf(“%d\n”,c2);
return 0;
}
7.给出三角形的三边长,求三角形的面积。
公式:若给定三角形的三边长,且任意两边之长大于第三边。
则:
area=√s(s - a)(s - b)(s - c)
其中s=(a+b+C)/2.
程序:
#include
#include
int main()
{
double a,b,c,area;
a=3.67;
b=5.43;
c=6.21;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“a=%f\tb=%f\tc=%f\n”,a,b,c);
printf(“area=%f\n”,area);
return 0;
}
8.求ax2+bx+c=0 方程的根。a,b,c 由键盘输入,设b2-4ac>0.
程序:
#include
#include
int main()
{
double a,b,c,disc,x1,x2,p,q;
scanf(“%lf%lf%lf”,&a,&b,&c);
disc=b*b-4*a*c;
if(disc<0)
printf(“This question has no real roots\n”);
else
{
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;
x2=p-q;
printf(“x1=%7.2f\nx2=%7.2f\n”,x1,x2);
}
return 0;
}
9.先后输出BOY 三个字符。
程序:
#include
int main()
{
char a='B',b='O',c='Y';
putchar(a);
putchar(b);
putchar(c);
putchar(‘\n');
return 0;
}
10.用三个getchar 函数先后向计算机输入BOY三个字符,然后用putchar 函数输
出。
程序:
#include
int main()
{
char a,b,c;
a=getchar();
b=getchar();
c=getchar();
putchar(a);
putchar(b);
putchar(c);
putchar(‘\n');
return 0;
}
或
#include
int main()
{
putchar(getchar());
putchar(getchar());
putchar(getchar());
putchar(‘\n');
return 0;