C Primer Plus (第6版)第五章 编程练习答案

1.

#include<stdio.h>
#define MIN 60
int main(void)
{
    int a,b,c;

    printf("Please enter minutes.\n");
    scanf("%d",&a);
    while (a > 0)
    {
        b = a / MIN;
        c = a % MIN;
        printf("%d minutes is %d hours and %d minutes.\n",a,b,c);
        scanf("%d",&a);
    }

    return 0;
}

2.

#include <stdio.h>
int main(void)
{
    int a,b;

    printf("Please enter an interger.\n");
    scanf("%d",&a);
    b=a+11;
    while (a<b)
    {

        printf("%d ",a);
        a++;

    }

    return 0;
}

3.

#include<stdio.h>
#define WEEK 7
int main(void)
{
    int a,b,c;

    printf("Please enter number of days.\n");
    scanf("%d",&a);
    while (a > 0)
    {
        b = a / WEEK;
        c = a % WEEK;
        printf("%d days is %d weeks and %d days.\n",a,b,c);
        scanf("%d",&a);
    }

    return 0;
}

4.

#include<stdio.h>
#define FEET 30.48
#define INCHES 2.54
int main(void)
{
    int a;
    float b,c;

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

    return 0;
}

5.

#include <stdio.h>
int main (void)
{
    int count,sum,days;

    count=0;
    sum=0;
    printf("Please enter days.\n");
    scanf("%d",&days);
    while (count++<days)
        sum=sum+count;
    printf("sum=%d\n",sum);

    return 0;
}

6.

#include <stdio.h>
int main (void)
{
    int count,sum,days;

    count=0;
    sum=0;
    printf("Please enter days.\n");
    scanf("%d",&days);
    while (count++<days)
        sum=sum+count*count;
    printf("sum=%d\n",sum);

    return 0;
}

7.

#include <stdio.h>
void cube(float n);
int main (void)
{
    float count;
    printf("Please enter a number of type float. \n");
    scanf("%f",&count);
    cube(count);

    return 0;
}
void cube(float n)
{

    printf("%f",n*n*n);
}

8.

#include <stdio.h>
int main (void)
{
    int first, second, c;
    
    printf("This program computer moduli.\n");
    printf("Enter an interge to serve as the second operand:");
    scanf("%d", &first);
    printf("Now enter the first operand:");
    scanf("%d", &second);
    printf("%d %% %d is %d \n",second,first,second % first);

    while(second>0)
    {
        printf("Enter an interge to serve as the second operand(<= 0 to quit):");
        scanf("%d",&second);
        if (second>0)
            printf("%d %% %d is %d \n",second,first,second%first);
        else
            printf("Done\n");
    }

    return 0;
}

9.

#include <stdio.h>
void Temperatures(float Fahrenheit1);
int main (void)
{
    float Fahrenheit1;
    
    printf("Please enter  Fahrenheit temperature: \n");
    while(scanf("%f",&Fahrenheit1) == 1)
    {
        Temperatures(Fahrenheit1);
    }

    return  0;
}
void Temperatures(float Fahrenheit1)
{
    const float C1 = 32.0;
    const float C2 = 5.0 / 9.0 * (Fahrenheit1 - C1);
    const float K1 = 273.16;
    const float K2 = C2 + K1;

    printf("Fahrenheit: %.2f  Celsius:%.2f  Kelvin:%.2f\n",Fahrenheit1,C2,K2 );
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值