第四次作业
- 编写程序,判断一个数n是正数还是负数
#include <stdio.h> main(){ int n; printf("请输入一个数"); scanf("%d",&n); if(n<0){ printf("%d是一个负数",n); } else{ printf("%d是一个正数",n); } }![]()
- 编写程序,计算出租车的行驶距离与费用之间的关系。起步3km内,8元;之后1.6/km。
#include <stdio.h>
main(){
float jvli;
float feiyong;
float chaoguo;
printf("请输入行驶距离:");
scanf("%f",&jvli);
chaoguo=8+1.6*(jvli-3);
if(jvli<=3){
printf("您需要支付8元");
}
else{
printf("您需要支付%f元",chaoguo);
}
}

3.
#include <stdio.h>
main(){
int a;
printf("请输入一个整数");
scanf("%d",&a);
if(a%2!=0){
printf("这是一个奇数");}
else{
printf("这是一个偶数");
}
}

4.输入一个数,输出它的绝对值。
#include <stdio.h>
main(){
int a;
printf("请输入一个整数");
scanf("%d",&a);
if(a>0){
printf("他的绝对值是%d",a);}
else{
printf("他的绝对值是%d\n",a*-1);
}
}

5.输入2个数,输出较大数。
#include <stdio.h>
main(){
int a,b;
printf("请输入两个整数");
scanf("%d %d",&a,&b);
if(a>b){
printf("较大数是%d",a);}
else{
printf("较大数是%d\n",b);
}
}

6(选做题) 输入一个字符,如果是大写就转成小写,如果是小写,就转成大写并输出.
#include <stdio.h>
main(){
char a;
printf("请输入一个字母");
scanf("%c",&a);
if(a>=97){
printf("他的大写是%c",a-32);}
else {
printf("他小写是%c\n",a+32);
}
}



浙公网安备 33010602011771号