/* convert4.c This program converts an integer greater than 100 but less than 1000 given by the user from the English unit of measure, inches, to the Metric System units; cm, m, and km. If the input is not between 100 and 1000, the user is prompted for correct input. This program will run until valid input is given. This program may not reflect all the requirements of the C Coding Standards used in this class. */ #include <stdio.h> int main() { int inches, meters; /* Notice that meters is an int this time, not a float. */ printf("This program will convert inches into the Metric System.\n"); printf("Please enter an integer between 100 and 1000: "); scanf("%d", &inches); while (inches < 100 || inches > 1000) { printf("Integer must be greater than 100 and less than 1000.\n"); printf("Please re-enter an inetger between 100 and 1000: "); scanf("%d", &inches); } /* This is explicit type casting. The multipication below produces a float for a result but it is being assigned to an int, so we type cast it so nothing is lost in the conversion. */ meters = (int)(inches * .0254); printf("Conversion of %d inches into metric yields:\n", inches); printf("\t\t%d centimeters\n", meters * 100); printf("\t\t%d meters\n", meters); printf("\t\t%d kilometers\n", meters / 1000); return(0); }


[foxtrot]> cc convert4.c [foxtrot]> a.out This program will convert inches into the Metric System. Please enter an integer between 100 and 1000: 5 Integer must be greater than 100 and less than 1000. Please re-enter an inetger between 100 and 1000: 10002 Integer must be greater than 100 and less than 1000. Please re-enter an inetger between 100 and 1000: 150 Conversion of 150 inches into metric yields: 300 centimeters 3 meters 0 kilometers [foxtrot]>