UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

scanf

scanf is used to get input from the user

The program

/*************************** ** File: cmtofeet.c ** Author: Sue Bogar ** Date: Jan 5, 1933 ** Section: 101 ** SSN: 123-45-6789 ** E-Mail: bogar@cs.umbc.edu ** ** This program reads in a length given in ** centimeters and converts it to its English ** equivalent in feet and inches. **************************/ #include <stdio.h> int main() { /* Declare variables */ double totalInches, cm, inch; int feet; /* Explain program & get a length in ** centimeters from the user */ printf("This program converts cm to feet & inches.\n"); printf("Length in centimeters? "); scanf("%lf", &cm); /* Calculate totalInches and use this to */ /* calculate feet and inches */ totalInches = cm / 2.54; feet = totalInches / 12; inch = totalInches - feet * 12; /* Print results */ printf("%f cm = %d ft %f in\n", cm, feet, inch); return 0; }

The Output

linux1[82]% gcc -Wall -ansi cmtofeet.c linux1[83]% a.out This program converts cm to feet & inches. Length in centimeters? 184 184.000000 cm = 6 ft 0.440945 in linux1[84]%% a.out This program converts cm to feet & inches. Length in centimeters? 178 178.000000 cm = 5 ft 10.078740 in linux1[85]% a.out This program converts cm to feet & inches. Length in centimeters? 179 179.000000 cm = 5 ft 10.472440 in linux1[86]% a.out This program converts cm to feet & inches. Length in centimeters? 180 180.000000 cm = 5 ft 10.866141 in


CSEE | 201 | 201 S'02 | lectures | news | help