c primer plus 第六版 第五章编程练习

(编译环境 Microsoft Visual Studio 2019)

1.

/*1.*/
#include<stdio.h>
#define M_PER_H 60
int main(void)
{
    int time;
    int hour, minute;

    printf("Please enter a time in minute.(<=0 to quit)\n");
    scanf_s("%d", &time);
    while (time > 0)
    {
        hour = time / M_PER_H;
        minute = time % M_PER_H;
        printf("the time is %d hours and %d minutes.\n", hour, minute);
        printf("\nPlease enter a time in minute.(<=0 to quit)\n");
        scanf_s("%d", &time);
    }
    printf("Done!\n");

    return 0;
}

2.

/*2.*/
#include<stdio.h>
int main(void)
{
    int value,object;

    printf("please enter a integer.\n");
    scanf_s("%d", &value);
    object = value + 10;
    while (value <= object)
    {
        printf("%d ", value);
        value++;
    }
    printf("\n");

    return 0;
}

3.

/*3.*/
#include<stdio.h>
#define A_WEEK 7
int main(void)
{
    int time;
    int weeks, days;

    printf("Please enter a number of days.(<=0 to quit)\n");
    scanf_s("%d", &time);
    while (time > 0)
    {
        weeks = time / A_WEEK;
        days = time % A_WEEK;
        printf("%d days are %d weeks, %d days.\n", time, weeks, days);
        printf("\nPlease enter a number of days.(<=0 to quit)\n");
        scanf_s("%d", &time);
    }
    printf("Done!\n");

    return 0;
}

4.

/*4.*/
#include<stdio.h>
#define C_PER_I 2.54f
#define I_PER_F 12
int main(void)
{
    float height_c,height_i;
    float inch;
    int feet;

    printf("Enter a height in centimeter:");
    scanf_s("%f", &height_c);
    while (height_c > 0)
    {
        height_i = height_c / C_PER_I;
        feet = height_i / I_PER_F;
        inch = height_i - (feet * I_PER_F);
        printf("%.1f cm = %d feet ,%.1f inches\n", height_c, feet, inch);
        printf("Enter a height in centimeter(<=0 to quit):");
        scanf_s("%f", &height_c);
    }
    printf("bye\n");

    return 0;
}

 

5.

/*5.*/
#include<stdio.h>
int main(void)
{
    int object;
    int count, sum;

    count = 0;
    sum = 0;
    printf("Please enter a object.\n");
    scanf_s("%d", &object);
    while (count++ < object)
    {
        sum = sum + count;
    }
    printf("sum=%d\n", sum);

    return 0;
}

 

6.

/*6.*/
#include<stdio.h>
int main(void)
{
    int object;
    int count, sum;
    int square;

    count = 0;
    sum = 0;
    printf("Please enter a object.\n");
    scanf_s("%d", &object);
    while (count++ < object)
    {
        square = count * count;
        sum = sum + square;
    }
    printf("sum=%d\n", sum);
    return 0;
}

 

7.

/*7.*/
#include<stdio.h>
void d_cube(double d);
int main(void)
{
    double value;

    printf("Please enter a double value.\n");
    scanf_s("%lf", &value);
    d_cube(value);

    return 0;
}

void d_cube(double d)
{
    long double cube;

    cube = d * d * d;
    printf("The cube is %le\n", cube);
}

 

8.

/*8.*/
#include<stdio.h>
int main(void)
{
    int second, first;

    printf("This program computes moduli.\n");
    printf("Enter an integer to serve as the second operand: ");
    scanf_s("%d", &second);
    printf("Now enter the first operand: ");
    scanf_s("%d", &first);
    while (first > 0)
    {
        printf("%d %% %d is %d\n", first, second, first % second);
        printf("Now enter the first operand(>=0 to quit): ");
        scanf_s("%d", &first);
    }
    printf("Done!\n");

    return 0;
}

 

9.

/*9.*/
#include<stdio.h>
void temperatures(double d);
int main(void)
{
    double fahrenheit;

    printf("Please enter a Fahrenheit temperature.(non-numeric to quit)\n");
    while (1 == scanf_s("%lf", &fahrenheit))
    {
        temperatures(fahrenheit);
        printf("\nPlease enter a Fahrenheit temperature.(non-numeric to quit)\n");
    }
    printf("bye\n");

    return 0;
}

void temperatures(double d)
{
    const float CF_FIRST = 5.0f / 9.0f;
    const float CF_SECOND = 32.0f;
    const float KC_TRANS = 273.16f;
    double kelvin, celsius;

    celsius = CF_FIRST * (d - CF_SECOND);
    kelvin = celsius + KC_TRANS;
    printf("Fahrenhrit temperature = %.2f\n", d);
    printf("Celsius temperature = %.2f\n", celsius);
    printf("Kelvin temperature = %.2f\n", kelvin);
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值