/* Name: recursiveMult.c Purpose: Implements recursive multiplication, a la Exercise 1-7 Author: Bill Slough Date: January 20, 2011 Input: a sequence of input lines of the form a b Output: a sequence of output lines of the form a b p where p = ab Notes: We use the recursive formulation of finding the product from Skiena, Chapter 1, using c = 2. */ #include int multiply(int y, int z); int main() { int a, b; /* A pair of input values */ int p; /* p = xy */ while (scanf("%d %d", &a, &b) != EOF) { /* Process and output one pair */ p = multiply(a, b); printf("%d %d %d\n", a, b, p); } return 0; } int multiply(int y, int z) { /* Return the product yz. Assumes y and z are non-negative. */ if (z == 0) return 0; else return multiply(2*y, z/2) + y*(z % 2); }