/* convert3.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 program exits. 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); if(inches < 100) { printf("Integer must be greater than 100. Good Bye!\n"); return(1); } else if(inches > 1000) { printf("Integer must be less than 1000. Good Bye!\n"); return(1); } /* 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 convert3.c [foxtrot]> a.out This program will convert inches into the Metric System. Please enter an integer between 100 and 1000: 2 Integer must be greater than 100. Good Bye! [foxtrot]> a.out This program will convert inches into the Metric System. Please enter an integer between 100 and 1000: 10002 Integer must be less than 1000. Good Bye! [foxtrot]> a.out This program will convert inches into the Metric System. Please enter an integer between 100 and 1000: 150 Conversion of 150 inches into metric yields: 300 centimeters 3 meters 0 kilometers [foxtrot]>