第四次作业

1.编写程序,判断一个数n是正数还是负数。

#include <stdio.h>
main() {
    int n;
    printf("请输入一个数:\n");
    scanf_s("%d", &n);
    if (n > 0)
        printf("n为正数");
    else
        if (n == 0)
            printf("n既不是正数也不是负数");
        else
            printf("n为负数");
}

  2.编写程序,计算出租车的行驶距离与费用之间的关系。起步3km内,8元;之后1.6/km。

 

#include <stdio.h>
main() {
    float juli;
    float feiyong;
    float chaoguo;
    printf("请输入行驶距离:");
    scanf_s("%f", &juli);
    chaoguo = 8 + 1.6 * (juli - 3);
    if (juli <= 3) {
        printf("您需要支付8元");
    }
    else 
        printf("您需要支付%f元", chaoguo);

    }

  3.输入一个数,判断是奇数还是偶数。

 

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

  4.输入一个数,输出他的绝对值。

 

#include<stdio.h>
main()
{
	int x;
	printf("输入一个数");
	scanf_s("%d", &x);
	if (x >= 0)
		printf("%d\n", x);
	else
		x = x * (-1);
	printf("%d\n", x);
}

  5.输入两个数,输出最大数。

 

#include<stdio.h>
main()
{
	int a, b;
	printf("请分别输入两个数并用空格隔开:\n");
	scanf_s("%d%d", &a, &b);
	if (a > b)
		printf("最大数是%d\n",a);
	else
		printf("最大数是%d\n",b);
}

  

 

 

 

 

posted @ 2021-11-01 23:45  鹏宇0240  阅读(42)  评论(0)    收藏  举报