• Chapter 5 :Operators,Expressions and Statements


    1. Write a program that converts time in minutes to time in hours  and  minutes.  Use #define or const to create a  symbolic constant for 60. Use a while loop to allow the user to enter values repeatedly and terminate the loop if a value for the time of 0 or less is entered.

    #include <stdio.h>
    
    int main(void) {
        const int minperhour = 60;
        int minutes, hours, mins;
        printf("Enter the number of minutes to convert: ");
        scanf("%d", &minutes);
        while (minutes > 0) {
            hours = minutes / minperhour;
            mins = minutes % minperhour;
            printf("%d minutes = %d hours, %d minutes
    ", minutes, hours, mins);
            printf("Enter next minutes value (0 to quit): ");
            scanf("%d", &minutes);
        }
        printf("Bye
    ");
        return 0;
    }

    3. Write a program that asks the user to enter the number of days and then converts that value to weeks and days. For example, it would convert 18 days to 2 weeks, 4 days. Display results in the following format: 

    18 days are 2 weeks, 4 days.

    Use a while loop to allow the user to repeatedly enter day values; terminate the loop when the user enters a nonpositive value, such as 0 or -20.

    #include <stdio.h>
    
    int main(void) {
        const int daysperweek = 7;
        int days, weeks, day_rem;
        printf("Enter the number of days: ");
        scanf("%d", &days);
        while (days > 0) {
            weeks = days / daysperweek;
            day_rem = days % daysperweek;
            printf("%d days are %d weeks and %d days.
    ", days, weeks, day_rem);
            printf("Enter the number of days (0 or less to end): ");
    
            scanf("%d", &days);
        }
        printf("Done!
    ");
        return 0;
    }

    5. Write a program that requests a type double number and prints the value of the number cubed. Use a function of your own design to cube the value and print it. The main() program should pass the entered value to this function.

    #include <stdio.h>
    
    void showCube(double x);
    
    int main(void) {
        double val;
        printf("Enter a floating-point value: ");
        scanf("%lf", &val);
        showCube(val);
        return 0;
    }
    
    void showCube(double x) {
        printf("The cube of %e is %e.
    ", x, x * x * x);
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    MYSQL的一些命令
    微信支付细节说明(服务商版本)
    MYSQL的一些概念
    MYSQL内置数据库之information_schema
    Lua5.1 三色标记gc
    LUA计算table大小getn
    游戏排行榜系统设计 -- 有感
    nginx如何跑起来
    C# winform datagridview数据绑定问题
    windows共享路径访问SMB安装
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829255.html
Copyright © 2020-2023  润新知