/*uses a conditional expression as well as a conditional statement*/
#include <stdio.h>
#include <ctype.h>
int main(void)
{
double fahr, cels;
int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/
/*get input type*/
printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: ");
scale = toupper(getchar());
/*get input, and do the appropriate calculation*/
printf("degrees? ");
scanf("%lf", (scale=='C')? &cels: &fahr);
if(scale == 'C') {
fahr = 9.0*cels/5.0+32.0;
} else {
cels = 5.0*(fahr-32.0)/9.0;
}
printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels);
return 0;
}
Next slide