C Program To Add Two Numbers

In this post, we are going to learn how to write a program to add two numbers in the C programming language.

After going through this tutorial, you will clearly understand how to do addition in C. 

C Program To Add Two Numbers

You will get to know two different methods to do this.

In the first example, the user is asked to enter two integers. Then, the sum of these two integers will be calculated and displayed on the screen.

And in the second example, the same output will be achieved using a user defined function.

So, Without further ado, let’s begin the tutorial.

C Program To Add Two Numbers

// C Program To Add Two Numbers
#include <stdio.h>
int main(){
    int a, b, sum;
    // Display Message Asking for Input
    printf("Enter Two Numbers: \n");

    // Asking for Input
    scanf("%d%d", &a, &b);

    sum = a + b;

    // Displays Output
    printf("The Sum of Two Numbers is: %d", sum);
    return 0;
}

Output

Enter Two Numbers: 
7
18
The Sum of Two Numbers is: 25

How Does This Program Work ?

    int a, b, sum;

In this program, we have declared three integers a, b and sum.

    scanf("%d%d", &a, &b);

The user is asked to enter two integers. The two integers are stored in a and b named variables respectively.

    sum = a + b 

Then, the two integers entered by the user are added using + operator and the result is stored in the sum variable.

    printf("The Sum of Two Numbers is: %d", sum);

Finally, the integer stored in the sum variable is displayed as output to the screen using printf() function.

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user. Here %d indicates that we are reading decimal/integer type.

printf() – printf() function is used to display and print the string under the quotation to the screen.

// – Used for Commenting in C

Using User Defined Function

In this program, we will write an addition logic in the user defined function sum() and we will be calling this function in the main function.

// C Program To Add Two Numbers Using User Defined Function
#include <stdio.h>

int sum(int x, int y){
    return x + y;
}
int main(){
    int a, b, c;
    printf("Enter First Integer: ");
    scanf("%d", &a);
    printf("Enter Second Integer: ");
    scanf("%d", &b);
    
    // Calling the User Defined Function 
    c = sum(a, b);
    printf("The Sum of Entered Numbers is: %d", c);
    return 0;
}

Output

Enter First Integer: 7
Enter Second Integer: 18
The Sum of Entered Numbers is: 25
int sum(int x, int y){
    return x + y;
}

Here in this program, we have generated an addition logic which provides us the addition of any two numbers. Then we are calling this function in the main function.

Conclusion

I hope after going through this tutorial, you have understood how to add two numbers in C Programming language. 

If you still have any doubt regarding this, feel free to comment down your query. We will be delighted to guide you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *