C Program to Add Two Numbers

 

Algorithm of Adding two Numbers

1 Step: START.

2 Step: Initialize integers A, B and C.

3 Step: Accept two integers A and B from User.

3 Step: Now do the operation using formula C=A+B.

4 Step: Now the addition is stored in C Variable.

5 Step: Print C.

6 Step: STOP.

Now we will do this arithmetic operation in C.There are different methods to make Addition in C we will see it one by one.

1. Addition Program in C

In this program, we will ask the user to enter the two numbers and do addition using formula. This addition will store in the third Variable and at the end, the value stored in the third variable is printed.

C

Output:

c program to add two numbers

2. Addition of Two Numbers Without Using Third Variable

In the first program after doing the addition of A and B we store that value in third variable C but, in this program, we will not use the third variable we will store value in same variable A.

C

Output:

program to add two numbers in c

This program is not recommended because we have initialized the variables A and B with 5 and 7 we didn’t take numbers from the user and the original value of A is lost.

3. C program to Add Two Numbers Repeatedly

The process of addition is same as program one only change is that first program end’s after doing one operation but in this program, we will use while loop which continues the addition until user press n for the exit.

C

Output:

addition using infinite method

4. Program to Add Two Numbers Using Function

Till now we do not use user-defined functions to calculate the sum of two numbers But, in this program, we will use a user-defined function to do the addition of two numbers.

C

Output:

using function

In the above program, we have used a user-defined function addition. When the compiler reaches to sum=addition(long a, long b) then compiler directly jumps to the function addition and perform the operation.

No comments