# C Program to Add Two Numbers

« Previous Program Next Program »

• Add two Numbers of Integer Type
• Add two Numbers of Floating-point Type
• Add two Numbers using Function
• Add two Numbers using Recursion

## Add two Numbers in C

First, we will learn about adding two numbers of Integer type. To add two numbers in C programming, ask from user to enter any two numbers. Place the addition of entered two number in a third variable of same data type. Finally print the value of third variable.

```#include<stdio.h>
#include<conio.h>
int main()
{
printf("Enter any two number: ");
scanf("%d%d", &num1, &num2);
printf("\nSum of %d and %d is %d", num1, num2, add);
getch();
return 0;
}```

As the above program was written under Code::Blocks IDE, therefore here is the initial snapshot of sample run:

Now supply any two number say 10 and 20, then press ENTER key to see the output as shown in the second snapshot of same sample run:

#### Steps used in above Program

Here are some of the main steps involved in above program:

• Declare any three variables say num1, num2 and add
• Here num1 and num2 holds first and second number entered by user, add will be used to store the addition result of given two number
• Now scan any two number entered by user
• Print the value of add as output

### Add Two Real Numbers in C

The above program is only applicable for integer value. If user supply any two number that contains decimal, then above program will not applicable in that case. So here is the modified version of adding two numbers in C without matter of whether entered number is an integer value or a real number.

```#include<stdio.h>
#include<conio.h>
int main()
{
printf("Enter any two number: ");
scanf("%f%f", &num1, &num2);
printf("\nSum of %0.2f and %0.2f is %0.2f", num1, num2, add);
getch();
return 0;
}```

The snapshot given here shows the sample run of above program:

As you can see from above program, in place of int, declare all the three variables of float type. To learn more about data type, refer to Data Types in C tutorial.

Here %0.2f is used to print real number upto 2 decimal place only.

## Add two Numbers in C using Function

Now let's create another program that does the same job but this time using function. Let's take a look at the program:

```#include<stdio.h>
#include<conio.h>
int sum(int, int);
int main()
{
printf("Enter any two number: ");
scanf("%d%d", &num1, &num2);
getch();
return 0;
}
int sum(int a, int b)
{
return a+b;
}```

Here is the sample run:

## Add two Numbers in C using Recursion

The question is write a program in C to add two numbers using recursive function. Let's take a look at the program first:

```#include<stdio.h>
#include<conio.h>
int sum(int, int);
int main()
{
printf("Enter any two number: ");
scanf("%d%d", &num1, &num2);
getch();
return 0;
}
int sum(int a, int b)
{
if(b==0)
return a;
else
return (1+sum(a, b-1));
}```

Here is the snapshot of sample run of above program:

To learn about recursion in detail, refer to Recursion in C tutorial.

#### Same Program in Other Languages

C Online Test

« Previous Program Next Program »