**Problem Analysis**

Addition is the simplest binary operation performed in mathematics. When the mathematical operation is performed on two objects we say that it is a binary operation.

Addition is one of the binary operations in mathematics that can be performed on Natural numbers, Integers, Rational numbers, Real Algebraic numbers and Real numbers.

Numbers that are combined to obtain a sum are called addends. The result of addition called sum is always greater than addends.

**Problem Description**

The problem is to write a program for the addition of two numbers in C language.

In C language the input is given by the user and it is accepted in a variable. An Addition program in C language requires two numbers and numbers can be Natural number, Integers, Rational numbers or Real Numbers.

Variables that accept values given by the user need to specify the type of value they can accept. In C language the type of value a variable can accept is specified by format specifier.

In C language “%d” is used for signed integers and “%f” for float values. Thus, “%d” can be used to take natural number input, integer input, whole number input from the user and “%f” values can be used to accept decimal number (real number) input from the user. C Language Addition Program can add values of two variables having format specifier %d or %f.

In addition to Natural Number, Integer, Rational Number and Real Numbers, the C language can also represent Binary Numbers, Octal Numbers and Hexadecimal Numbers. Binary Numbers are composed of 0 or 1. 0 or 1 are integers and can be represented using %d. Octal Numbers in C language are represented by %o and variable accepting octal numbers must have %o as format specifier. Hexadecimal number in C language has %x or %X format specifier. A variable accepting a Hexadecimal number must have %x or %X as format specifier. C language must also perform addition on two Octal Number and two Hexadecimal Number.

Thus, C language Addition Program must perform addition on –

- Two Natural Number, Integers having format specifier %d.
- Two Real numbers (decimal values) or rational numbers having format specifier %f.
- Two Binary Numbers having format specifier %d.
- Two Octal numbers having format specifier %o.
- Two Hexadecimal Numbers having format specifier %x or %X.

### Solution to Problem

This section deliver C language Addition Program in the order as follows:

- Two Natural Number, Integers having format specifier %d.
- Two Real numbers (decimal values) or rational numbers having format specifier %f.
- Two Binary Numbers having format specifier %d.
- Two Octal numbers having format specifier %o.
- Two Hexadecimal Numbers having format specifier %x or %X.

## C Language Addition Program – Two Natural Number or Two Integers

#include <stdio.h> int main() { int num1, num2, summ; printf("Enter two Integers or Two Natural Numbers: "); scanf("%d %d", &num1, &num2); // calculating sum summ = num1 + num2; printf("%d + %d = %d", num1, num2, summ); return 0; }

Output: Enter two Integers or Two Natural Numbers: 12 34 12 + 34 = 46

Code Analysis: This code performs an addition program on two integers or two natural numbers. Input of integer number or natural number is taken in variables num1 and num2. Num1 and num2 are declared as integers. Num1 and Num2 have format specifiers %d. The result of addition is stored in variable summ. Summ is a user defined variable having data type integer and format specifier %d.

## C Language Addition Program – Addition of Two Real Numbers or Two Rational Numbers

#include <stdio.h> int main() { float num1, num2, summ; printf("Enter two Real Numbers or Two Rational Numbers expressed in decimal notation: "); scanf("%f %f", &num1, &num2); // calculating sum summ = num1 + num2; printf("%f + %f = %f", num1, num2, summ); return 0; }

Output: Enter two Real Numbers or Two Rational Numbers expressed in decimal notation: 12.23 34.56 12.230000 + 34.560001 = 46.790001

Code Analysis This code performs addition on two Real Numbers or two Rational Numbers expressed in decimal. Input of two Real Number or Rational Number expressed in decimal is taken in variables num1 and num2, num1 and num2 are declared as float to work on decimal numbers. num1 and num2 have format specifiers %f. The result of addition is stored in variable summ. Summ is a user defined variable having data type float and format specifier %f.

## C Language Addition Program – addition of two Binary Numbers

#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:In this code binary number input is taken from the user in two variables bin1 and bin2 of data type long integer.

Binary number is added in while loop. While loop is executed until binary number bin1 or bin2 is not equal to 0. Following is the code of while loop:

while(bin1!=0||bin2!=0)

Addition is done in the while loop. For addition modulo division is carried out on binary numbers - bin1 and bin2. Addition of binary number added to remainder variable rem. rem is declared as integer variable and initialize to 0. Following is the code:

summ[i++] = (bin1 %10 + bin2 %10 + rem ) % 2;

rem is obtained by modulo division on bin1 and bin2 and adding the result to rem variable after this whole is divided by 2. Following is the code:

rem = (bin1 %10 + bin2 %10 + rem ) / 2;

Remaining number is taken in variable bin1 and bin2 by dividing it by 10. Following is the code:

bin1 = bin1/10;

bin2 = bin2/10;Obtained sum of binary numbers is stored in array sum[20]. Following is the code:

if(rem!=0)

summ[i++] = rem;

--i;Sum of two binary numbers is displayed on screen using a while loop. Following is the code to print values of array sum:

printf("Addition of two binary numbers: ");

while(i>=0)

printf("%d",summ[i--]);

## C Language Addition Program – Addition of Two Octal Numbers

#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: This code adds two octal numbers. Octal number input is taken in variable octtt1 and octtt2. Addition of Octal number is done in a while loop. In the while loop modulo division 10 is carried out to find remainder and variable maxxx is divided by 10 to obtain a quotient. Division by 10 is done to give base 10 significance to addition. This is done by code: first_temp = maxxx%10; maxxx=maxxx/10; second_temp = miiin%10; miiin = miiin/10; When this is done, modulo division 8 is carried out to find the remainder. Division by 8 is done because the octal number has base 8. Carry is obtained by dividing variables first_temp, second_temp and carrrry by 8. Following is the code for this: carrrry = (first_temp+second_temp+carrrry)/8; Sum is obtained by adding the value variables pooo, miiid and suuum.

## C Language Addition Program – addition of two Hexadecimal Numbers

#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("Enter first Hexadecimal: "); scanf("%s", hex1); printf("Enter 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: In this code two hexadecimal character arrays are declared of size 100. User input is taken in the character arrays hex1[100] and hex2[100]. Two for loops are used in this code for two character arrays hex1[100] and hex2[100]. Inside the for loop hex1[100] and hex2[100] are checked for integer digit 0 to 9. This is done by following code: if(hex1[i]>='0' && hex1[i]<='9') If this condition is satisfied then the value of hex1[i] is subtracted with 0 otherwise it is subtracted with ASCII code of “A” and 10 is added to obtain 10’s place of digit. This done by following code: r=hex1[i]-'0'; r=hex1[i]-'A'+10; Since it is hexadecimal it has base 16. Variable decimal1 is added to the value of r obtained in the previous step.value of r is multiplied by output of pow function. In the Hexadecimal system the position of every digit is a power of 16. Each digit in the hexadecimal number system is 16 times greater than the previous digit. This is done by following code: decimal1 = decimal1 +r*pow(16,p); After this, the value of variable p is incremented. Variable decimal1 gives the decimal number of its corresponding Hexadecimal number. Similarly decimal2 gives the decimal number of its corresponding Hexadecimal number. Values of two variables decimal1 and decimal2 are added to obtain a sum. This is done by: sum = decimal1 + decimal2; The value of the variable sum is in decimal which is converted back to hexadecimal by following code: rem = quotient % 16; if(rem =0; ii--) { printf("%c", hex3[ii]); }

## Leave a Reply