C program to add two numbers

Write a C program to input two numbers from user and calculate their sum. C program to add two numbers and display their sum as output. How to add two numbers in C programming.

Example
Input

Input first number: 20
Input second number: 10

Output

Sum = 30

Required knowledge

Arithmetic operators, Data types, Basic Input/Output

Program to add two numbers

/**
 * C program to find sum of two numbers
 */

#include <stdio.h>

int main()
{
    int num1, num2, sum;
    
    /*
     * Read two numbers from user
     */
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number:");
    scanf("%d", &num2);
    
    /* Adding both number is simple and fundamental */
    sum = num1 + num2;
    
    /* Prints the sum of two numbers */
    printf("Sum of %d and %d = %d", num1, num2, sum);
    
    return 0;
}

Note: You can also write the above program using single scanf() function.

/**
 * C program to find sum of two numbers
 */

#include <stdio.h>

int main()
{
    int num1, num2, sum;
    
    /*
     * Input two numbers from user
     */
    printf("Enter any two numbers : ");
    scanf("%d%d", &num1, &num2);
    
    sum = num1 + num2;
    
    /* Prints the sum of two numbers */
    printf("Sum of %d and %d = %d\n", num1, num2, sum);
    
    return 0;
}

Note: \n is a escape sequence character used to print new line (used to move to the next line).

For advanced learning - Program to find sum of two numbers using pointers.

Output

Enter any two numbers : 12
20
Sum of 12 and 20 = 32

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>