The next program uses the formula oC=(5/9)(oF-32) to print the following table of Fahrenheit temperatures and their centigrade or Celsius equivalents:
(important: helps to know how to apply formula based programs in c.)
1 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
It is different from the first programm and difficult too. It consists of new c language codes.
#include
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature scale */
upper = 300; /* upper limit */
step = 20; /* step size */
fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
getch();
return 0; /* if you will not type this, the programm will give you an error. (it will return some value.)*/
}
#include
/* ... */ --- Any characters between /* and */ are ignored by the compiler; they may be used freely to make a program easier to understand.
main() -------------- Function
{ ... } -------- Curle Braces in which statement is performed.
int .... ; integer value to declared first
while( ....... ){ ............ } ------- The while loop operates as follows: The condition in parentheses is tested. If it is true (fahr is less than or equal to upper), the body of the loop (the three statements enclosed in braces) is executed. Then the condition is re-tested, and if true, the body is executed again. When the test becomes false (fahr exceeds upper) the loop ends. There are no further statements in this program, so it terminates. The body of a while can be one or more statements enclosed in braces.
printf(" ..... "); ----- Print characters
(..... <= .....) -------- Greater than equal too
\t -------- To form Table

No comments:
Post a Comment