C Program to Add Two Numbers

Hello readers, in this post we will learn how to add two numbers using C Programming language.

C Program to Add Two Numbers

This program asks the user to enter two numbers, then it computes the sum of those two numbers using the plus (+) operator.

We will add two numbers using the following methods:

  1. Using Standard Method
  2. Using User Function

So, without further ado, let’s begin this tutorial.

C Program to Add Two Numbers

C Program

// C Program to Add Two Numbers
#include <stdio.h>
int main(){
    int a, b, sum;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    
    // Adding two numbers
    sum = a + b;
    
    // Displaying output
    printf("Sum of two numbers: %d", sum);
    return 0;
}

Output

Enter the first number: 12
Enter the second number: 13
Sum of two numbers: 25
C Program to Add Two Numbers

Explanation

    int a, b, sum;

In this program, we have declared three int data type variables named a, b and sum.

    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);

Then, the user is asked to enter two numbers. The input is taken with the help of scanf() function.

    // Adding two numbers
    sum = a + b;

We add these two numbers with the help of the plus (+) operator. The result is stored in the sum named variable.

    // Displaying output
    printf("Sum of two numbers: %d", sum);

Finally, the result is displayed on the screen using printf() function.

C Program to Add Two Numbers Using Functions

C Program

// C Program to Add Two Numbers Using Functions
#include <stdio.h>
int sumOfTwo(int a, int b){
    return a + b;
}

int main(){
    int num1, num2, sum;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    
    // Calling out function
    sum = sumOfTwo(num1, num2);
    
    // Displaying output
    printf("Sum of two numbers: %d", sum);
    return 0;
}

Output

Enter the first number: 7
Enter the second number: 8
Sum of two numbers: 15
C Program to Add Two Numbers Using Functions

Explanation

int sumOfTwo(int a, int b){
    return a + b;
}

In this program, we have defined a custom function named sumOfTwo which returns the sum of two numbers.

    // Calling out function
    sum = sumOfTwo(num1, num2);

Then, we call the custom function in the main function. This gives us the sum of two numbers.

    // Displaying output
    printf("Sum of two numbers: %d", sum);

And the result is printed on the screen using printf() function.

Conclusion

I hope after reading this post, you understand how to add two numbers using C Programming language.

If you have any doubt regarding this program, let us know in the comment section. We will be delighted to solve your query.

Leave a Comment