C - The C Answer (2nd Edition) - Exercise 1-3

/*
Modify the temperature conversion program to print a heading above the table.
*/

#include <stdio.h>

/* print fahrenheit-Celsius table for fahr = 0, 20, ..., 300; floating-point
version */
main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;   /* lower limit of temperature table */
    upper = 300; /* upper limit                      */
    step  = 20;  /* step size                        */

    printf("Fahr Celsius\n");
    fahr = lower;
    while (fahr <= upper)
    {
    	celsius = (5.0 / 9.0) * (fahr - 32.0);
    	printf("%3.0f %6.1f\n", fahr, celsius);
    	fahr += step;
    }
}

/* Output:

Fahr Celsius
  0  -17.8
 20   -6.7
 40    4.4
 60   15.6
 80   26.7
100   37.8
120   48.9
140   60.0
160   71.1
180   82.2
200   93.3
220  104.4
240  115.6
260  126.7
280  137.8
300  148.9
Press any key to continue . . .

*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 15:18:58

C - The C Answer (2nd Edition) - Exercise 1-3的相关文章

C - The C Answer (2nd Edition) - Exercise 1-18

/* Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline); int remove(char s[]);

C - The C Answer (2nd Edition) - Exercise 1-19

/* Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline);

C - The C Answer (2nd Edition) - Exercise 1-4

/* Write a program to print the corresponding Celsius to Fahrenheit table. */ #include <stdio.h> /* print Celsius-Fahrenheit table for celsius = 0, 20, ..., 300; floating-point version */ main() { float fahr, celsius; int lower, upper, step; lower =

C - The C Answer (2nd Edition) - Exercise 1-5

/* Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. */ #include <stdio.h> /* print Fahrenheit-Celsius table in reverse order */ main() { int fahr; for(fahr = 300; fahr >= 0; fahr -= 20

C - The C Answer (2nd Edition) - Exercise 1-7

/* Write a program to print the value of EOF. */ #include <stdio.h> main() { printf("EOF is %d\n", EOF); } /* Output: EOF is -1 */ 版权声明:本文为博主原创文章,未经博主允许不得转载.

C - The C Answer (2nd Edition) - Exercise 1-8

/* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, tabs, and newlines */ main() { int c, nb, nt, nl; nb = 0; /* number of blanks */ nt = 0; /* number of tabs */ nl = 0; /* number of newlines */ while((c =

C - The C Answer (2nd Edition) - Exercise 1-9

/* Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. */ #include <stdio.h> #define NONBLANK 'a' /* replace string of blanks with a single blank */ main() { int c, lastc; lastc = NONBLANK;

C - The C Answer (2nd Edition) - Exercise 1-10

/* Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. */ #include <stdio.h> #define NONBLANK 'a' /* replace tabs an

C - The C Answer (2nd Edition) - Exercise 1-1

/* Run the "hello, world" program on your system. Experiment with leaving out parts of the program to see what error messages you get. */ #include <stdio.h> main() { printf("hello world\n"); } 版权声明:本文为博主原创文章,未经博主允许不得转载.

C - The C Answer (2nd Edition) - Exercise 1-2

/* Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above. */ #include <stdio.h> main() { printf("hello world\y"); printf("hello world\7"); printf("hello wor