# Problem Analysis

Integers are numbers which can be positive, negative or Zero. An integer can not be a fraction. On integers arithmetic operations such as addition, subtraction, multiplication and division are performed. Examples of integers include -12,-4,0,2,1,4,7,5,56 etc. thus, integers are whole numbers composed of zero, positive numbers and negative numbers.

Addition of two positive integers is a positive integer and addition of two negative integers is a positive integer.

# Adding two integers having same sign

To add two integers having the same sign – absolute values of integers are added and the result is written with the sign provided with the numbers.

For example,

(+1) + (+3) = +4

If two integers to be added have different signs than subtract the absolute value of two integers, the result obtained is written with the sign of the integer having largest absolute values.

For example,

(-8) + (+2) = – 6

(+9) + (-2) = + 7

Arithmetic operators in C language perform mathematical operations such as addition, subtraction, multiplication, division on variables or constants. Arithmetic operator + calculates addition on two variables of data type integer, float or double.

# Adding two integers having same sign

To add two integers having the same sign – absolute values of integers are added and the result is written with the sign provided with the numbers.

For example,

(+1) + (+3) = +4

If two integers to be added have different signs than subtract the absolute value of two integers, the result obtained is written with the sign of the integer having largest absolute values.

For example,

(-8) + (+2) = – 6

(+9) + (-2) = + 7

Arithmetic operators in C language perform mathematical operations such as addition, subtraction, multiplication, division on variables or constants. Arithmetic operator + calculates addition on two variables of data type integer, float or double.

Here, *b* is a positive integer greater than 1. Any positive integer x can be written as:

x = a_{l}b^{l} + a_{l-1}b^{l-1} + . . . + a_{1}b+a_{0}

where, *l* is a positive integer, 0 ≤ a_{j} ≤ b for j =0,1,. . .,*l* and a_{l} ≠ 0.

Thus, 214 is in base 10 and can be written as 21221 in base 3, but 214 and 21221 both are integers.

In the same way, (214)_{10} in octal will be (326)_{8} and (D6)_{16 }in Hexadecimal. Integers can also be written in hexadecimal format having base 16. Hexadecimal uses standard digit 0 thru 9 plus letters “A” through “F”.

## Problem Solution

Following are the code for addition of two integers.

**C Program to add two integer having base 10**

```
#include <stdio.h>
int main()
{
int num1, num2, summ;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
summ = num1 + num2;
printf("%d + %d = %d", num1, num2, summ);
return 0;
}
```

Output: Enter two integers: 12 34 12 + 34 = 46

Code Analysis: Above code calculates addition of two integers having base 10. Three integer type variables - num1, num2 and summ are declared. Integer input is taken from the user in variable num1 and num2 and result is stored in variable summ. Addition is performed by following code: summ = num1 + num2 Result obtained is displayed on screen by printf( ) statement. printf("%d + %d = %d", num1, num2, summ);

**C Program to add two integers having base 2**

```
#include<stdio.h>
int main()
{
long int bin1,bin2;
int i=0,rem = 0,summ[20];
printf("Input first binary number: ");
scanf("%ld",&bin1);
printf("Input second binary number: ");
scanf("%ld",&bin2);
while(bin1!=0||bin2!=0){
summ[i++] = (bin1 %10 + bin2 %10 + rem ) % 2;
rem = (bin1 %10 + bin2 %10 + rem ) / 2;
bin1 = bin1/10;
bin2 = bin2/10;
}
if(rem!=0)
summ[i++] = rem;
--i;
printf("Addition of two binary numbers: ");
while(i>=0)
printf("%d",summ[i--]);
return 0;
}
```

Output: Input first binary number: 101 Input second binary number: 101 Addition of two binary numbers: 1010

Code Analysis: Above code adds two integers having base 2. Five integer variables are declared - bin1, bin2, i, rem and summ[20]. Binary number Input is taken from the user in variable bin1 and bin2. Addition is done in while loop having terminating condition: bin1 != 0 || bin2 != 0 Addition is done by executing following code: summ[i++] = (bin1 %10 + bin2 %10 + rem ) % 2; rem = (bin1 %10 + bin2 %10 + rem ) / 2; bin1 = bin1/10; bin2 = bin2/10;

**C Program to add two integer having base 8**

```
#include <stdio.h>
int main()
{
int octtt1, octtt2;
int first_temp, second_temp;
int maxxx,miiin,carrrry=0,suuum=0,miiid,iii=0,pooo=1;
printf("Enter first octal number : ");
scanf("%d", &octtt1);
printf("Enter second octal number : ");
scanf("%d", &octtt2);
if(octtt1>octtt2)
{
maxxx = octtt1;
miiin = octtt2;
}
else
{
maxxx=octtt2;
miiin=octtt1;
}
while(maxxx>0)
{
first_temp = maxxx%10;
maxxx=maxxx/10;
second_temp = miiin%10;
miiin = miiin/10;
miiid = (first_temp+second_temp+carrrry)%8;
carrrry = (first_temp+second_temp+carrrry)/8;
suuum=pooo*miiid+suuum;
pooo=pooo*10;
if(maxxx==0 && carrrry>0)
{
suuum = pooo*carrrry+suuum;
}
}
printf("Octal Number Addition = %d", suuum);
return 0;
}
```

Output: Enter first octal number : 6 Enter second octal number : 5 Octal Number Addition = 13

Code Analysis: Above code adds two integers having base 8. Octal number input is taken from user in two integer variable octtt1 and octtt2. Addition is done in while loop by executing following code: first_temp = maxxx%10; maxxx=maxxx/10; second_temp = miiin%10; miiin = miiin/10; miiid = (first_temp+second_temp+carrrry)%8; carrrry = (first_temp+second_temp+carrrry)/8; suuum=pooo*miiid+suuum; pooo=pooo*10; if(maxxx==0 && carrrry>0) { suuum = pooo * carrrry + suuum; }

**C Program to add two integer having base 16**

```
#include <stdio.h>
#include <string.h>
#include <math.h>
int main( )
{
char hex1[100], hex2[100];
int p = 0,q=0;
int decimal1 = 0, decimal2 = 0;
int r, i, j, sum=0;
long decimalnum, quotient, rem;
int ii, jj = 0;
char hex3[100];
printf("Enetr first Hexadecimal: ");
scanf("%s", hex1);
printf("Enetr second Hexadecimal: ");
scanf("%s", hex2);
for(i=strlen(hex1)-1;i>=0;--i)
{
if(hex1[i]>='0' && hex1[i]<='9')
{
r=hex1[i]-'0';
}
else
{
r=hex1[i]-'A'+10;
}
decimal1 = decimal1 +r*pow(16,p);
++p;
}
for(j=strlen(hex2)-1;j>=0;--j)
{
if(hex2[j]>='0' && hex2[j]<='9')
{
r=hex2[j]-'0';
}
else
{
r=hex2[j]-'A'+10;
}
decimal2 = decimal2 +r*pow(16,q);
++q;
}
sum = decimal1 + decimal2;
quotient = sum;
//printf("\n Quotient is: %ld", quotient);
while(quotient != 0)
{
rem = quotient % 16;
if(rem < 10)
{
hex3[jj++] = 48 + rem;
}
else
{
hex3[jj++] = 55 + rem;
}
quotient = quotient/16;
}
jj--;
printf("Sum of two hexadecimal number is ");
for(ii=jj; ii>=0; ii--)
{
printf("%c", hex3[ii]);
}
return 0;
}
```

Output: Enter first Hexadecimal: F Enter second Hexadecimal: C Sum of two hexadecimal number is 1B

Code Analysis: Above code adds two hexadecimal numbers. Since hexadecimal numbers can have the alphabet as input from the user, two character arrays are declared hex1[100] and hex2[100]. Addition is done by converting hexadecimal input into decimal number by executing the following code: for(i=strlen(hex1)-1;i>=0;--i) { if(hex1[i]>='0' && hex1[i]<='9') { r=hex1[i]-'0'; } else { r=hex1[i]-'A'+10; } decimal1 = decimal1 +r*pow(16,p); ++p; } for(j=strlen(hex2)-1;j>=0;--j) { if(hex2[j]>='0' && hex2[j]<='9') { r=hex2[j]-'0'; } else { r=hex2[j]-'A'+10; } decimal2 = decimal2 +r*pow(16,q); ++q; } Addition is done on this decimal number and stored in integer variable sum. Obtained result is converted back to hexadecimal by following code: rem = quotient % 16; if(rem < 10) { hex3[jj++] = 48 + rem; } else { hex3[jj++] = 55 + rem; } quotient = quotient/16; Result is displayed on the output window by following code: for(ii=jj; ii>=0; ii--) { printf("%c", hex3[ii]); }

## CONCLUSION

**C Program to add two integers **can be done in the following ways:

- C Program to add two integer having base 10
- C Program to add two integer having base 2
- C Program to add two integer having base 8
- C Program to add two integer having base 16

An integer can be represented in different base, thus program to add two integer must consider addition of integer having different base.

## Leave a Reply