The name of your C program file must be called longadd.c, files with any other name will not be marked.
At this early stage of the course, you would probably have written a program that involves some integer addition. Below is an example of a very simple program add.c that accepts two integers from the user, computes and displays their sum in long addition format.
/**********************************************************************/
/* Matric Number: U051234A and U059876B */
/* Userid: u0501234 and u0509876 */
/* (For those doing pair programming, only ONE of you should submit */
/* your program. The other student should NOT submit.) */
/* Lab: 1 */
/* Lab Group Number: 99 */
/* Lab TA's Name: Who needs a lab TA when you're Bill Gates? */
/* Lab Session Date: 15 February 2006 */
/* Lab Session Time: 0800 - 0945 */
/* Title: More Addition */
/* Purpose: Calculates the sum of two integers. */
/**********************************************************************/
#include <stdio.h>
int main()
{
/* Variable declaration */
int num1, num2;
int result;
/* Get user input */
printf("Enter the first number: ");
scanf("%i", &num1);
printf("Enter the second number: ");
scanf("%i", &num2);
/* Compute and pretty print result */
result = num1 + num2;
printf(" %4i\n"
"+ %4i\n"
"------\n"
" %5i\n", num1, num2, result);
return 0;
}
Throughout this lab exercise, we shall restrict the user input to non-negative numbers that do not exceed four digits in length, i.e. the user input must be between 0 to 9999 inclusive. A sample output from add.c is given below. User input is denoted in bold.
Enter the first number: 1234 Enter the second number: 9876 1234 + 9876 ------ 11110
1
1234
+ 9876
------
0
Working through for all columns, the complete picture is as follows.
1111 1234 + 9876 ------ 11110For simplicity, the carry can be represented as a number by itself, i.e.
11110 1234 + 9876 ------ 11110
& gcc -Wall longadd.c -o longadd
$ longadd
Enter the first number: 1234
Enter the second number: 9876
11110
1234
+ 9876
------
11110
$ longadd
Enter the first number: 9999
Enter the second number: 9999
11110
9999
+ 9999
------
19998
$ longadd
Enter the first number: 123
Enter the second number: 98
110
123
+ 98
------
221
$ longadd
Enter the first number: 1234
Enter the second number: 0
0
1234
+ 0
------
1234
$ longadd
Enter the first number: 1234
Enter the second number: 2345
0
1234
+ 2345
------
3579
$
A total of 27 different hosts have accessed this document in the last 556 days; your host, 216.73.216.129, 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.