CS1101C Lab 1 (Even Week)

Long Addition

The deadline for this lab question is Thursday 16 February 2006, 23:59:59 hours.

The name of your C program file must be called longadd.c, files with any other name will not be marked.

Preliminary

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

Long Addition With Carry

For completeness, we also need to include the carry. For example, when adding 4 and 6, we have always been taught to place 0 below the column and carry 1 onto the top of the left adjacent column as such
    1
  1234
+ 9876
------
     0
Working through for all columns, the complete picture is as follows.
 1111
  1234
+ 9876
------
 11110
For simplicity, the carry can be represented as a number by itself, i.e.
 11110
  1234
+ 9876
------
 11110

Initial Task

Your task is to include the carry within the long division. A sample run of the program is given below. User input is denoted in bold.
& 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

$

Points to Note


This document, index.html, has been accessed 15 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 9 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 4 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.