codescracker
c

C Program to Add Two Numbers



« Previous Program Next Program »

In this article, you will learn and get code about how to add two numbers in C. Here are the list of ways that is used here in this article to add two numbers in C:

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.

// C Program to add two numbers
// -----codescracker.com-----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num1, num2, add;
    printf("Enter any two number: ");
    scanf("%d%d", &num1, &num2);
    add = 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:

c program to add two numbers

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:

add two numbers in c

Steps used in above Program

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

What if User enters Real number ?

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 mannering whether entered number is an integer value or a real number.

// C Program to add two real numbers
// -----codescracker.com-----

#include<stdio.h>
#include<conio.h>
int main()
{
    float num1, num2, add;
    printf("Enter any two number: ");
    scanf("%f%f", &num1, &num2);
    add = 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:

add two real numbers in c

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:

// C Program to add two numbers using function
// -----codescracker.com-----

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

Here is the sample run:

c program add two number using function

To learn more about function in c, refer to Function in C tutorial article.

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:

// C Program to add two numbers using recursion
// or recursive function
// -----codescracker.com-----

#include<stdio.h>
#include<conio.h>
int sum(int, int);
int main()
{
    int num1, num2, add;
    printf("Enter any two number: ");
    scanf("%d%d", &num1, &num2);
    add = sum(num1, num2);
    printf("\nSum = %d", add);
    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:

add two numbers in c using recursion

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

Same Program in Other Languages


« Previous Program Next Program »