Chapter 3 : Data and C - Programming Exercises

1.    Find out what your system does with integer overflow, floating-point overflow, and floating-point underflow by using the experimental approach; that is, write programs having these problems.

#include <stdio.h>

int main(void) {
    int integerNum = 1234567890987654;
    float floatNum = 1234567890987654321.0f;
    float smallNum = 0.00001;

    printf("%d\n",integerNum);
    printf("%f\n",floatNum);
    printf("%f\n",smallNum/200);
    return 0;
}

The output on my PC is:

1016588934
1234567939550609408.000000
0.000000

2.    Write a program that asks you to enter an ASCII code value, such as 66, and then prints the character having that ASCII code.  

#include <stdio.h>

int main(void) {
    char ch;

    printf("Enter the character: ");
    scanf("%c", &ch);
    printf("%c's ASCII code is %d.\n", ch, ch);

    return 0;
}

4.    Write a program that reads in a floating-point number and prints it first in decimal-point notation, then in exponential notation, and then, if your system supports it, p notation. Have the output use the following format (the actual number of digits displayed for the exponent depends on the system): 

Enter a floating-point value:  64.25   
fixed-point notation: 64.250000 
exponential notation: 6.425000e+01  
p notation: 0x1.01p+6 
#include <stdio.h>

int main(void) {
    float number;

    printf("Enter a float-pointing number:");
    scanf("%f", &number);
    printf("fixed-point notation: %f\n", number);
    printf("exponential notation: %e\n", number);
    printf("p notation: %a\n", number);
}

5.    There are approximately 3.156 × 10 7  seconds in a year. Write a program that requests your age in years and then displays the equivalent number of seconds.  

#include <stdio.h>

#define seconds_in_a_year 3.156e7

int main(void) {
    int age;

    printf("Enter your age in years:");
    scanf("%d", &age);
    printf("Your age in seconds is %lld", age * seconds_in_a_year);
}

6.    The mass of a single molecule of water is about 3.0×10 -23  grams. A quart of water is about 950 grams. Write a program that requests an amount of water, in quarts, and displays the number of water molecules in that amount.    

#include <stdio.h>

#define mass_of_a_single_molecule_of_water 3.0e-23

int main(void) {
    int amount;

    printf("Enter the amount of water in quarts:");
    scanf("%d", &amount);
    printf("The number of water molecules: %lld", amount * 950 / mass_of_a_single_molecule_of_water);

    return 0;
}

7.    There are 2.54 centimeters to the inch. Write a program that asks you to enter your height in inches and then displays your height in centimeters. Or, if you prefer, ask for the height in centimeters and convert that to inches.    

#include <stdio.h>

int main(void) {
    int inch;

    printf("Enter your height in inches:");
    scanf("%d", &inch);
    printf("%d inches is %.2f centimeters.\n", inch, inch * 2.54);

    return 0;
}

8.    In the U.S. system of volume measurements, a pint is 2 cups, a cup is 8 ounces, an ounce is 2 tablespoons, and a tablespoon is 3 teaspoons. Write a program that requests a volume in cups and that displays the equivalent volumes in pints, ounces, tablespoons, and teaspoons. Why does a floating-point type make more sense for this application than an integer type?

#include <stdio.h>

int main(void) {
    float cup;

    printf("Enter the volume in cups:");
    scanf("%f", &cup);

    float pint = cup / 2;
    float ounce = cup * 8;
    float tablespoon = ounce * 2;
    float teaspoon = tablespoon * 3;

    printf("%f cups is %f pints, or %f tablespoons,or %f teaspoons", cup, pint, tablespoon, teaspoon);

    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九成宫醴泉铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值