C Primer Plus(第6版)第五章答案

1.
#include <stdio.h>
#define minute 60

int main (void)
{
    int Minute;

    printf ("Please input time in minute: \n");
    scanf ("%d", &Minute);
    
    while(Minute > 0)
    {
        printf ("time is %d : %d\n", Minute / minute, Minute % minute);
        printf ("Please input time in minute: \n");
        scanf ("%d", &Minute);
    }

    return 0;
}


2.

#include <stdio.h>

int main (void)
{
    int num;
    int i;

    printf ("Please input a number: ");
    scanf ("%d", &num);

    for (i = 0; i <= 10; i++)
    {
        printf ("%d\n", num + i);
    }

    return 0;
}

3.

#include <stdio.h>
#define days 7

int main (void)
{
    int day;

    printf ("Please input days: ");
    scanf ("%d", &day);

    while (day > 0)
    {
        printf ("%d days are %d weeks, %d days.\n", day, day / days, day %days);
        printf ("Please input days: ");
        scanf ("%d", &day);
    }

    return 0;
}

4.

#include <stdio.h>
#define inch 2.54

int main (void)
{
    float height;

    printf ("Enter a height in centimeters: ");
    scanf ("%f", &height);

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

    return 0;
}

5.

#include <stdio.h>

int main (void)
{
    int day, sum = 0;
    int i;

    printf ("Please input days: ");
    scanf ("%d", &day);

    for (i = 1; i <= day; i++)
    {
        sum += i;
    }

    printf ("sum = %d", sum);

    return 0;
}

6.

#include <stdio.h>

int main (void)
{
    int day, sum = 0;
    int i;

    printf ("Please input days: ");
    scanf ("%d", &day);

    for (i = 1; i <= day; i++)
    {
        sum += i * i;
    }

    printf ("sum = %d", sum);

    return 0;
}

7.

#include <stdio.h>

double cube(double num);

int main (void)
{
    double num;

    printf ("Please input a num: ");
    scanf ("%lf", &num);

    printf ("The cube of %lf is %lf", num, cube(num));

    return 0;
}


double cube(double num)
{
    return num * num * num;
}

8.

#include <stdio.h>

int main (void)
{
    int a, b;

    printf ("This program computes moduli.\n");
    printf ("Enter am integer to serve as the second operand: ");
    scanf ("%d", &a);
    printf ("Now enter the first operand: ");
    scanf ("%d", &b);

    while (b > 0)
    {
        printf ("%d %% %d is %d\n", b, a, b % a);
        printf ("Enter next number for first operand (<= 0 to quit): ");
        scanf ("%d", &b);
    }

    return 0;
}

9.

#include <stdio.h>

void Temperatures (double F);

int main (void)
{
    double F;

    printf ("Enter a temperature: ");
    while (scanf ("%lf", &F))
    {
        Temperatures (F);
        printf ("Enter a temperature(Enter 'q' to quit): ");
    }


    return 0;
}


void Temperatures (double F)
{
    double K, C;
    const double a = 5.0 / 9.0, b = 32.0, c = 273.16;

    C = a * (F - b);
    K = C + c;

    printf ("F: %.2f\nC: %.2f\nK: %.2f\n", F, C, K);

    return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值