|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e6/solution/p2.c
#include <stdio.h>
/**
* Make a table for Miles to Kilometers
* 0) Use a tab character between the columns
* 1) Print column headings for the Miles and Kilometers columns.
* 3) Miles will be an integer from 10 to 300 in steps of 10 and Kilometers
* should a floating point value that is printed to 2 decimal places.
* Kilometers = Miles * 1.609344
* 4) Kilometers should be printed to two decimal places.
* - You must use a loop for this assignment.
* Example output:
* Miles: Kilometers:
* 10 16.09
* 20 32.19
* 30 48.28
* 40 64.37
* ...
* 300 482.80
*/
int main(void)
{
int miles;
float kilometers;
int lower, upper, step;
// Do again the same as we did in p1.c:
lower = 10;
upper = 300;
step = 10;
miles = lower;
// We print the "header" outside of the loop:
printf("Miles:\tKilometers:\n");
while (miles <= upper) {
kilometers = miles * 1.609344;
printf("%d\t%.2f\n", miles, kilometers);
miles = miles + step;
}
return 0;
}
|