C examples for Preprocessor:Macro
The harmonic mean of two numbers is obtained by taking the inverses of the two numbers, averaging them, and taking the inverse of the result.
#include <stdio.h> #define HMEAN(X,Y) (2.0 * (X) *(Y) / ((X) + (Y))) int main(void) { double x, y, ans; //w w w.j a v a2 s .co m puts("Enter a pair of numbers (q to quit): "); while (scanf("%lf %lf", &x, &y) == 2) { ans = HMEAN(x,y); printf("%g = harmonic mean of %g %g.\n", ans, x, y); printf(" see if works with arithmetic expressions \n "); ans = HMEAN(x + y, x * y); printf("%g = harmonic mean of %g %g.\n", ans, x + y, x * y); puts("Enter a pair of numbers (q to quit): "); } puts("Bye"); return 0; }