C语言作业题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.第一次
1.1.编写一程序,通过键盘输入一华氏温度,将其转换为摄氏温度后输出。公式为c=5/9(f -32) #include "stdio.h"
main()
{
fl oat f=0,h=0;
printf("请输入华氏温度:");
scanf("%f",&f);
printf("转化为摄氏温度为:");
h=(fl oat)5/9*(f-32);
printf("%.2f\n",h);
}
1.2.通过键盘输入一小写字母,将其转换为大写字母输出,并输出该大写字母的后一位字母和前一位字
母(如果输入该大写字母为A,则其前一位字母为Z;如果输入该大写字母为Z,则其后一位字母为A)。
#include "stdio.h"
main()
{
char ch1,ch2,ch3,ch;
printf("请输入一个小写字母:\n");
whil e(scanf("%c",&ch),ch<97 || ch>122)
{
printf("您输入的符号不是小写字母,请重新输入一个小写字母:\n");
}
printf("其对应的大写字母和前后的大写字母分别为:\n");
if(ch=='a'){ch2=ch-32;ch1=ch2+25;ch3=ch2+1;}
else if(ch=='z'){ch2=ch-32;ch1=ch2-1;ch3=ch2-25;}
else {ch2=ch-32;ch1=ch2-1;ch3=ch2+1;}
printf("%c,%c,%c\n",ch1,ch2,ch3);
}
1.3.通过键盘输入一个两位的整数,试编写程序将其个位与十位分别以字符的方式输出。输出形式为:
“the input number is 你输入的整数,the tens is 十位数字,the units is 个位数字。”如果输入的整数不正确,则输出“wrong input”
#include "stdio.h"
main()
{
int a,b1,b2;
printf("请输入一个两位正整数:\n");
scanf("%d",&a);
if(a<10||a>=100)
{
printf("输入不符合要求");
}
else
{
b1=a%10;
b2=a/10;
printf("十位数是:%c\n个位数是:%c\n",b2+'0',b1+'0');
}
}
1.4.给定方程ax^2+bx+c=0,试编写程序根据键盘输入实数a、b、c,能输出其根,要求:
1.5.如果该方程有两个不同的根,则输出形式为"the Answer of ax^2+bx+c=0 is x1=根1,x2=根2"
1.6.如果该方程有两个相同的根,则输出形式为"the Answer of ax^2+bx+c=0 is x1=X2=根"
1.7.如果该方程无解,则输出形式为"the Answer of ax^2+bx+c=0 is none",其中a、b、c均为键盘输入,
输出结果保留两位小数。
#include "stdio.h"
#include "math.h"
main(){
float a,b,c,disc,x1,x2;
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc==0){
x1=x2=-b/(2*a);
printf("the Answer of ax^2+bx+c is x1=x2=%.2f\n",x1);
}
else if(disc>0){
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("the Answer of ax^2+bx+c is x1=%.2f,x2=%.2f\n",x1,x2);
}
else{printf("the Answer of ax^2+bx+c=0 is none\n");}
}
2.第二次
2.1.求1/3+3/5+5/7+…前20项之和。(本题要求分别以goto,for,do ...whil e,whil e四种循环语句实现) #include
int main()
{
int i;
float s=0;
for(i=1;i<=20;i++){
s+=(float)(2*i-1)/(2*i+1);
}
printf("s=%f\n",s);
return 0;
}
#include
int main()
{
int p,r,n,m;
printf("n,m:");
scanf("%d,%d",&n,&m);
if(p=n*m)
while(m!=0)
{r=n%m;
n=m;
m=r;}
printf("最大公约数是%d\n",n);
printf("最小公倍数是%d\n",p/n);
return 0;
}
#include
void main()
{
int s,i,j;
int a;
printf("请输入一个数:\n");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
s=0;
for(j=1;j
{
if(i%j==0)s=s+j;
}
if(i==s)
{
printf("完全数%d=",i);
for(j=1;j
if(i%j==0)
printf("%d+",j);
printf("\b \n");
}
}
}
2.2.输入两个正整数,求其最小公倍数。