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);
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    js/Jquery table 内部控件计算和tr的生成
    反射 实体类的赋值/取值问题
    C#读取Excel 几种方法的体会
    水晶报表升级遇到的问题
    使用异或位运算符实现交换两个整数详解
    美国宇航局两万兆数据存储方案下载
    Flash全屏(转载)
    Unity3D实现动态加载游戏资源
    C#,ASP.NET jquery uploadify上传控件中文乱码解决办法
    HTML通过button触发inputfile控件上传文件的问题
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829255.html
Copyright © 2011-2022 走看看