/* --------------------------------------------------------------------- // Figure 7.26: Computing resistance. // --------------------------------------------------------------------- */ #include void main( void ) { int r1, r2; /* integer input variables for two resistances */ double r1d, r2d; /* double variables for two resistances */ double r_eq; /* equivalent resistance of r1 and r2 in parallel */ printf( "\n Enter integer value of resistances #1 and #2 (ohms): " ); scanf( "%i%i", &r1, &r2 ); printf( " r1 = %i r2 = %i \n", r1, r2 ); r_eq = (r1 * r2) / (r1 + r2); /* Oops! Integer division. */ printf( "\n The truncated resistance value is %g\n", r_eq ); r_eq = ((double)r1 * r2) / (r1 + r2); /* Better, we cast first. */ printf( " We cast to double first and get %g\n", r_eq ); r1d = r1; /* Coerce to type double... */ r2d = r2; /* by copying into double variables */ r_eq = (r1d * r2d) / (r1d + r2d); /* and compute using doubles. */ printf( " The true value of equivalent resistance is %g\n\n", r_eq ); }