zoukankan      html  css  js  c++  java
  • 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);
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    [LeetCode] 101. 对称二叉树
    [LeetCode] 394. 字符串解码!!!!
    USACO Ordered Fractions
    USACO The Castle
    遇到的Mysql的一个坑
    USACO-palsquare 遇到的一个坑
    大整数相乘
    vs2012扩展
    JS实现文字倒计数
    jqAutoComplete 和 knockout
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829255.html
Copyright © 2011-2022 走看看