Computer Science 202
Systems Programming Concepts Using C
Lab 2 - Math Trick
Winter 1998


Write a C program using the GNU C Compiler (gcc) that will interact with the user to perform a simple math trick.


The math trick works as follows:

Operation Result
1. Ask the user to enter a positive integer. 1234
2. Determine the sum of the digits in the number. 10
3. Subtract the sum from the original number. 1224
4. Ask the user to re-arrange the digits. 4212
5. Add 25 to the number. 4237
6. Ask the user to remove one of the non-zero digits from the number. 427
7. Determine the sum of the digits in the number. 13
8. Subtract 7 from the sum of the digits. 6
9. Subtract the result from the next highest multiple of 9. 9 - 6 = 3

The trick is that the final value (3 in the above case) is the digit that the user eliminated from the number. That's it. (Sorry if you expected more.) You are to write a C program that performs the above trick. A portion of your grade will be based in the quality of your interactive program.


Before the end of class on 2nd Tuesday you must demonstrate to your instructor that your program successfully adds up the digits of an integer.

The following must be delivered to your instructor via text-only email no later than midnight of 3rd Monday January 19, 1998.

  1. Source code for your program.
In addition, you must deliver to the instructor at the start of class (Lab) on 3rd Tuesday, January 20, 1998 the following:
  1. A printed copy of your source code.
  2. An example of an interactive session you used to test your program.


  1. When you create your C program give it a name with a .c extension, prog1.c or trick.c for example.
  2. To compile your C program use the gcc command from the command line, for example: gcc prog1.c
  3. If your program compiles successfully a file named a.out will be constructed.
  4. To run your program simply type a.out at the command line.
  5. Other compilation options:
  6. Use the standard input and output streams for your I/O.
  7. Proper use of programming style is required. A correctly running program with gross style violations can be worth as little as 25\% of the assigned grade.
  8. Programs are graded as follows: The mailed source is compiled, with all warning messages enabled. A program which does not compile is worth zero points. If it compiles, it is run on your test data. It is also run on the instructors data. At this point, a raw grade (range 0-50) is assigned. Your source code is then examined for adherence to programming standards. Your final grade is then your raw grade minus any deductions for style.
  9. You are expected to do all of your own code writing, but you may help others with algorithm design and debugging of already written code. If, when assisting others, you write statements that resemble C code, you are probably providing too much help.