资源描述
第四章——例十
输入三角形的三边长,求三角形面积。求三角形面积的公式为:area=s(s-a)(s-b)(s-c),其中s=(a+b+c)/2
#include <stdio.h>
#include <math.h>
void main()
{
float a,b,c,s,area;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("a=%7.2f, b=%7.2f,c =%7.2f,s =%7.2f\n",a,b,c,s);
printf("area=%7.2f\n",area);
}
第四章——例十一
从键盘中输入一个大写字母,要求改用小写字母输出
#include <stdio.h>
void main()
{
char c1,c2;
c1=getchar();
printf("%c,%d\n",c1,c1);
c2=c1+32;
printf("%c,%d\n",c2,c2);
}
第四章——习题8
设圆半径r=1.5,圆柱高h=3,求圆周长,圆面积,圆球表面积,圆球体积,圆柱体积。用scanf输入数据,输出计算结果,输出时要求有文字说明,取小数点后2位数字。
#include<stdio.h>
void main()
{
float h,r,l,s,sq,vq,vz;
float pi=3.1415926;
printf(“请输入圆半径r,圆柱高h:”);
scanf(“%f,%f”,&r,&h);
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
vq=4.0/3.0*pi*r*r*r;
vz=pi*r*r*h;
printf(“圆周长为: l=%6.2f\n”,l);
printf(“圆面积为: s=%6.2f\n”,s);
printf(“圆球表面积位:sq=%6.2f\n”,sq);
printf(“圆球体积为: q=%6.2f\n”,vq);
printf(“圆柱体积为: vz=%6.2f\n”,vz);
}
第四章——习题9
输入一个华氏温度,要求输出摄氏温度,输出时要求有文字说明,取小数点后两位数字。公式为c=5/9(f-32)
#include <stdio.h>
void main()
{
float c,f;
printf("请输入一个华氏温度:");
scanf("%f",&f);
c=(5.0/9.0)*(f-32);
pritnf("摄氏温度为:%5.2f\n",c);
}
编程求圆面积、圆周长。要求圆半径由键盘输入,输出时要求有文字说明,取小数点后两位数字。
#include <stdio.h>
void main()
{
float r, l, s;
float pi=3.1415926;
printf("表输入圆半径r:");
scanf("%f", &r);
l=2*pi*r;
s=pi*r*r;
printf("圆周长为:l=%6.2f\n", l);
printf("圆面积为:s=%6.2f\n", s);
}
编程求圆柱体积,圆柱表面积。要求圆半径,圆柱高由键盘输入,输出时要求有文字说明,取小数点后两位数字。
#include <stdio.h>
void main()
{
float r, h, v, s;
float pi=3.1415926;
printf("表输入圆半径r、圆柱高h:");
scanf("%f, %f", &r,&h);
v=pi*r*r*h;
s=2*pi*r*r+2*pi*r*h;
printf("圆柱体积为:v=%6.2f\n", v);
printf("圆柱表面积为:s=%6.2f\n", s);
}
第五章——例一
输入两个实数,将这两个数按由大到小的顺序排列,并输出这两个数。
#include <stdio.h>
void main()
{
float a,b,t;
scanf("%f,%f",&a,&b);
if(a<b)
{
t=a;a=b;b=t;
}
printf("%5.2f,%5.2f\n",a,b);
}
第五章——例三
有一函数:当x<0时,y=-1;当x=0时,y=0;当x>0时,y=1。编一程序,输入x的值,输出y的值。
#include <stdio.h>
void main ()
{
int x,y;
scanf("%d",&x);
if(x<0)
y=-1;
else if(x==0)
y=0;
else y=1;
printf("x=%d,y=%d\n",x,y);
}
第五章——例四
输入一个字符,判断它是否是大写字母,如果是,将它转换成小写字母;如果不是,不转换。然后输出最后得到的字符。
#include <stdio.h>
void main ( )
{
char ch;
scanf("%c",& ch);
ch=(ch>='A'&& ch<='Z')?(ch+32):ch;
printf("%c\n",ch);
}
第五章——例五
写程序,判断某一年是否是闰年
#include<stdio.h>
void mian()
{
int year,leap;
scanf(“%d”,&year);
if(year%4==0)
{
if(year%100==0)
{
If(year%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
if(leap)
printf(“%d is”,year);
else
printf(“%d is not”,year);
printf(“a eap year.\n”);
}
第五章——习题4
有三个整数a,b,c,由键盘输入,输出其中最大者。
#include <stdio.h>
void main()
{
int a, b, c;
printf("请输入3个整数:");
scanf("%d,%d,%d", &a, &b, &c);
if (a<b)
if (b<c)
printf("max=%d\n, c);
else
printf("max=%d\n, b);
else if(a<c)
printf("max=%d\n, c);
else
printf("max=%d\n, a);
}
第五章——习题5
有一函数:当x<1时,y=|x|;当1<=x<10时,y=2x-1;当x>=10时,y=3x+11。
编一程序,输入x的值,输出y的值。
#include <stdio.h>
#include <math.h>
void main()
{
int x, y;
printf("输入x:");
scanf("%d",&x);
if (x<1)
y=abs(x);
else if (x<10)
y=2*x-1;
else
y=3*x-11;
printf("x=%3d, y=%d\n",x,y);
}
第六章——例二
编程求1+2+3+…+100的值,并输出结果。
#include <stdio.h>
void main()
{
int i,sum=0;
i=1;
while (i<=100)
{
sum=sum+i;
i++;
}
printf("%d\n",sum);
}
第六章——例八
编程判断整数m是否是素数,并输出结果
#include <stdio.h>
#include <math.h>
void main()
{
int m,i,k;
scanf("%d",&m);
k=sqrt(m);
for (i=2;i<=k;i++)
if(m%i==0) break;
if(i>k)
printf("%d is a prime number\n",m);
else
printf("%d is not a prime number\n",m);
}
第六章——习题1
输入两个正整数m和n,求其最大公约数和最小公倍数。
#include<stdio.h>
void main()
{
int p,r,n,m,temp;
printf("请输入n和m: ");
scanf("%d,%d,%d",&n,&m);
if(n<m)
{
temp=n;
n=m;
m=temp;
}
p=n*m;
while(m!=0)
{
r=n%m;
n=m;
m=r;
}
printf(":%d\n",n);
printf(" :%d\n",p/n);
}
第六章——习题2
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
#include<stdio.h>
void main()
{
char c;
int letters=0,space=0,digit=0,other=0;
printf("\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<'z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf(":%d\n:%d\n:%d\n:%d\n",letters,space,digit,other);
}
第六章——习题6
输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。
#include<stdio.h>
void main()
{
int i,j,k,n;
printf("narcissus numbers are");
for(n=100;n<1000;n++)
{
i=n/100;
j=n/10-i*10;
k=n%10;
if(n==i*i*i+j*j*j+k*k*k)
printf("%5d",n);
}
printf("\n");
}
第七章——例四
将一个二维数组行和列的元素互换,存到另外一个二维数组中。
#include <stdio.h>
void main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;
printf("array a:\n");
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
printf("%5d",a[i][j]);
b[j][i]=a[i][j];
}
printf("\n");
}
printf("array b:\n");
for (i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
printf("%5d",b[i][j]);
printf("\n");
}
}
第七章——例五
有一个3乘4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号。
#include <stdio.h>
void main()
{
int i,j,row=0,colum=0,max;
int a[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}};
max=a[0][0];
for (i=0;i<=2;i++)
for (j=0;j<=3;j++)
if (a[i][j]>max)
{
max=a[i][j];
row=i;
colum=j;
}
printf("max=%d,row=%d,colum=%d\n",max,row,colum);
}
第七章——习题5
将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,4,3,2,0。要求改为0,2,3,4,6,8。
#include<stdio.h>
#define N 5
void main()
{
int a[N],i,temp;
printf("enter array a:\n");
for(i=0;i<N;i++)
scanf("%d",&a[i]);
printf("array a:\n");
for(i=0;i<N;i++)
printf("%4d",a[i]);
for(i=0;i<N/2;i++)
{
temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
printf("\n Now,array a:\n");
for(i=0;i<N;i++)
printf("%d",a[i]);
printf("\n");
}
第八章——例二
调用函数时的数据传递,写一个函数,求两个整数中的较大者,用主函数调用这个函数,并输出结果,两个整数由键盘输入。
#include <stdio.h>
void main()
{
int max(int x,int y);
int a,b,c;
scanf("%d,%d",&a,&b);
c=max(a,b);
printf("max is %d",c);
}
int max(int x,int y)
{
int z;
z=x>y?x:y;
return(z);
}
第八章——例七
有5个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人多少岁?他说比第3个人大2岁。问第3个人多少岁?他说比第2个人大2岁。问第2个人多少岁?他说比第1个人大2岁。最后问第1个人,他说是10岁。请问的第5个人多大。
用递归方法解决该问题。
#include <stdio.h>
int age(int n)
{
int c;
if(n= =1) c=10;
else c=age(n-1)+2;
return(c);
}
void main()
{
printf("%d",age(5));
}
第八章——例八
用递归方法求n!。
#include <stdio.h>
void main()
{
float fac(int n);
int n;
float y;
printf("input an integer number:");
scanf("%d",&n);
y=fac(n);
printf("%d!=%10.0f\n",n,y);
}
float fac(int n)
{
float f;
if(n<0) {printf("n<0,dataerror!");}
else if(n==0 || n==1) f=1;
else f=fac(n-1)*n;
return(f);
}
求1!+2!+3!+……+20!
#include<stdio.h>
void main()
{
float s=0,t=1;
int n;
for(n=1;n<=20;n++)
{
t=t*n;
s=s+t;
}
printf(“结果为:%e”,s);
}
编一程序,将两个字符串连接起来,不使用strcat函数
#include<stdio.h>
void main()
{
char s1[80],s2[40];
int i=0,j=0;
scanf(“%s”,s1);
scanf(“%s”,s2);
while(s1[i]!=’\0’)
i++;
while(s2[j]!=’\0’)
s1[i++]=s2[j++];
s1[i]=’\0’;
printf(“The new string is:%s”,s1);
}
10
展开阅读全文