C Program to Add Two Numbers

In this article, we will write a C program to add two numbers. The program takes two integers from the user as input, calculates their sum and prints the result on the output window.

Sample Example:

Input:
Enter the First Number: 10
Enter the Second Number: 20

Output:
The sum of two numbers is: 30

To find the sum of two numbers in C, we can use the addition operator(+). This operator takes two operands and returns their sum as a result.

As we want to take the two integers from the user as input, we have to declare two variables let’s say those variables are num1, and num2 of int type.

To store the sum of these two variables, we need one more variable sum which will also be of type int.

See implementation in the following C program:

// C program to add two numbers

#include <stdio.h>

int main() {

    int num1, num2, sum;
    
    printf("Enter the First Number: ");
    scanf("%d", &num1);
    
    printf("Enter the Second Number: ");
    scanf("%d", &num2);
    
    // Calculate the sum
    sum = num1 + num2;
    
    // Print the sum
    printf("The sum of two numbers is: %d", sum);
    
    return 0;

}

Output:

Enter the First Number: 10
Enter the Second Number: 20
The sum of two numbers is: 30

Program Explanation:

In the above program, the user is first asked to enter two numbers with the help of the printf() function. The first number is stored in the variable num1 and the second number is stored in the variable num2.

To take inputs from the user we have used the built-in function scanf(). As we want to take only integer inputs from the user, therefore we have used %d format specifier.

After taking the numbers from the user, we have used the below statement;

sum = num1 + num2;

In this statement, we are adding two numbers num1, num2 and storing the result in the sum variable.

Finally, the printf() function is used to show the result of the addition of two numbers.

I hope you will find this article helpful. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment