Table Of Contents
Previous Section Next Section

Program 79: Square Deal

C++ doesn't have a power operator, so we define our own macro to compute X2. We've decided to test this macro by printing the squares of the numbers from 1 to 10. But what do we really print?

  1 /************************************************
  2  * Print out the square of the numbers          *
  3  *      from 1 to 10.                           *
  4  ************************************************/
  5 #include <iostream>
  6
  7 /*********************************** ************
  8  * macro to square a number.                    *
  9  ************************************************/
 10 #define SQR(x) ((x) * (x))
 11
 12 int main()
 13 {
 14     int number; // The number we are squaring
 15
 16     number = 1;
 17
 18     while (number <= 10) {
 19         std::cout << number << " squared is " <<
 20             SQR(++number) << std::endl;
 21     }
 22
 23     return (0);
 24 }

(Next Hint 200. Answer 88.)

Table Of Contents
Previous Section Next Section