C Primer Plus Sixth Edition (Chinese Edition) Chapter 5 (Perfect Revised Edition) Programming Practice Answers

//The code written by this blogger is only for readers' reference;

//If you have any shortcomings, please raise them, and the blogger will do their best to modify;

//Attach the programming practice questions after class;

//If it is useful to you, please like or share it with others;



//5.11 - 1.c

#include <stdio.h>
#define MIN_PER_HOUR 60

int main(void)
{
    
    
    int m, hour, min;

    printf("Please enter a number for minute (<= 0 to quit): ");
    while ((scanf("%d", &m) == 1) && (m > 0))
    {
    
    
        hour = m / MIN_PER_HOUR;
        min = m % MIN_PER_HOUR;
        printf("%d minutes = %d hours and %d minutes.\n", m, hour, min);
        printf("You can enter again (<= 0 to quit): ");
    }
    printf("Done.\n");

    return 0;
}

//-------------

//5.11 - 2.c

#include <stdio.h>
#define LEN 10

int main(void)
{
    
    
    int n;
    int i = 0;

    printf("Please enter a number: ");
    scanf("%d", &n);

    printf("Print %d to %d:\n", n, n + 10);
    while (i <= LEN)
    {
    
    
        printf("%d\n", n + i);
        i++;
    }
    printf("Done.\n");

    return 0;
}

//-------------

//5.11 - 3.c

#include <stdio.h>
#define DAYS_PER_WEEK 7

int main(void)
{
    
    
    int n, week, days;

    printf("Please enter days (<=0 to quit): ");
    while ((scanf("%d", &n) == 1) && (n > 0))
    {
    
    
        week = n / DAYS_PER_WEEK;
        days = n % DAYS_PER_WEEK;
        printf("%d days are %d weeks, %d days.\n", n, week, days);
        printf("You can enter again (<=0 to quit): ");
    }

    return 0;
}

//-------------

//5.11 - 4.c

#include <stdio.h>
#define CM_PER_INCH 2.54f
#define CM_PER_FEET 30.48f

int main(void)
{
    
    
    int feet;
    float cm, inch;

    printf("Enter a height in centimeters: ");
    while ((scanf("%f", &cm) == 1) && (cm > 0))
    {
    
    
        feet = (int)(cm / CM_PER_FEET);
        inch = (cm - CM_PER_FEET * feet) / CM_PER_INCH;
        printf("%.1f cm = %d feet, %.1f inches\n", cm, feet, inch);
        printf("Enter a height in centimeters (<=0 to quit): ");
    }
    printf("bye\n");

    return 0;
}

//-------------

//5.11 - 5.c

#include <stdio.h>

int main(void)
{
    
    
    int n, sum, count;
    sum = 0;

    printf("Enter the number of days you work: ");
    scanf("%d", &count);
    n = count;
    while (count > 0)
    {
    
    
        sum += count;
        count--;
    }
    printf("You earned $%d in %d days.\n", sum, n);

    return 0;
}

//-------------

//5.11 - 6.c

#include <stdio.h>

int main(void)
{
    
    
    int n, sum, count;
    sum = 0;

    printf("Enter the number of days you work: ");
    scanf("%d", &count);
    n = count;
    while (count > 0)
    {
    
    
        sum += count * count;
        count--;
    }
    printf("You earned $%d in %d days.\n", sum, n);

    return 0;
}

//-------------

//5.11 - 7.c

#include <stdio.h>

void cube(double c);

int main(void)
{
    
    
    double n;

    printf("Please you enter a double number: ");
    scanf("%lf", &n);
    cube(n);

    return 0;
}

void cube(double c)
{
    
    
    printf("The cube of %g is %g.\n", c, c * c * c);
    return;
}

//-------------

//5.11 - 8.c

#include <stdio.h>

int main(void)
{
    
    
    int f_o, s_o;

    printf("This program computes moduli.\n");
    printf("Enter an integer to serve ");
    printf("as the second operand: ");
    scanf("%d", &s_o);
    printf("Now enter the first operand: ");
    while ((scanf("%d", &f_o) == 1) && (f_o > 0))
    {
    
    
        printf("%d %% %d is %d\n", f_o, s_o, f_o % s_o);
        printf("Enter next number for first operand (<= 0 to quit): ");
    }
    printf("Done\n");

    return 0;
}

//-------------

//5.11 - 9.c

#include <stdio.h>

void Temperatures(double f_t);

int main(void)
{
    
    
    double temp;

    printf("Please enter a temperature number (q to quit): ");
    while (scanf("%lf", &temp) == 1)
    {
    
    
        Temperatures(temp);
        printf("You can enter again (q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

void Temperatures(double f_t)
{
    
    
    const double f_val = 32.0;
    const double k_val = 273.16;
    double t = 5.0 / 9.0 * (f_t - f_val);
    //↑摄氏温度 = 5.0 / 9.0 * (华氏温度 - 32);
    double k_t = t + k_val;
    //↑开氏温度 = 摄氏温度 + 273.16;
    printf("Centigrade temperature: %.2lf°C\n", t);
    printf("Fahrenheit temperature: %.2lf°C\n", f_t);
    printf("Kelvin temperature: %.2lf°C\n", k_t);

    return;
}

//-------------

//----------------------------April 3, 2020-------------- -----------------

Guess you like

Origin blog.csdn.net/m0_46181359/article/details/105291212