CS1101C Lab 1 (Even Week)
Multiplication
The deadline for this lab question is Wednesday 14 September 2005,
23:59:59 hours.
The name of your C program file must be called
multiply.c, files with any other name will not be
marked.
You are expected to spend a maximum of 2 hours for this assignment.
The Task
Nemo's kid wants to learn long multiplication. Gosh, it has been ages
since you've did it! Now with calculators and computers, you merely
press the numbers into the device and the answer pops out.
But kids must learn long multiplication the long way. Nemo has totally
forgotten how to do it; so he asks you to write a program that shows
long multiplication.
Here are two sample runs of the program that Nemo wants. Note that $
indicates the Unix prompt, and you are reminded to follow the sample
runs exactly, else marks will be deducted. User input is
underlined.
$ gcc -Wall multiply.c -o multiply
$ ./multiply
Enter two integers to multiply: 9876 5432
9876
x 5432
---------
49380
39504
29628
+ 19752
---------
53646432
---------
$ ./multiply
Enter two integers to multiply: 2345 608
2345
x 608
---------
0
14070
0
+ 18760
---------
1425760
---------
$
Let's look at the first example:
- 9876 is multiplied with 5 to give 49380. It is shifted three places
to the left since it shows the value in thousands.
- 9876 is multiplied with 4 to give 39504. It is shifted two places
to the left since it shows the value in hundreds.
- 9876 is multiplied with 3 to give 29628. It is shifted one place
to the left since it shows the value in tens.
- 9876 is multiplied with 2 to give 19752. It is not shifted.
- The answer of 53646432 is obtained by adding up the above four
intermediate values (after multiplying each intermediate value with an
appropriate number if necessary).
You are not allowed to obtain the final answer directly by calculating
9876 * 5432.
You may assume that the numbers entered by the user will be between 0 to
9999 inclusive.
This document, index.html, has been accessed 25 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 19 different hosts have accessed this document in the
last 445 days; your host, 216.73.216.39, has accessed it 1 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.