Addtion of two numbers in C

C Program to Add two Integer numbers

Given two numbers num1 and num2. The task is to write a program to find the addition of these two numbers.

Example,

Input: num1 = 10, num2 = 30
Output: 40

 

In the below C program to add two integer numbers, the program is first asked to enter two numbers. The input number is scanned using the scanf() function. The scanned input is stored in the variables num1 and num2. Then, the variables num1 and num2 are added using the arithmetic operator +, and the result is stored in the variable sum.

 

#include<stdio.h>

int main()
{
    int num1, num2, sum = 0;

    // Ask user to enter the two numbers
    printf("Enter two numbers num1 and num2 : \n");

    // Read two numbers from the user
    scanf("%d%d", &num1, &num2);

    // Calclulate the addition of num1 and num2
    // using '+' operator
    sum = num1 + num2;

    // Print the sum
    printf("Addition of num1 and num2 is: %d", sum);

    return 0;
}

Output:

Enter two numbers num1 and num2:  5  6
Addition of num1 and num2 is: 11

 

Watch this video to see how to add two integer numbers using the C Language.

 

C program to add two integer numbers using a function:

It is quite simple like the above program. The logic to adding two numbers will be the same but here we will create a separate function to perform the addition. If you don’t know how to create a function then you can read this article “How to create a function in C“.

#include<stdio.h>

int addTwoNumber(int x, int y)
{
    //Addition of two number
    return (x+y);
}

int main()
{
    int num1, num2, sum = 0;
    // Ask user to enter the two numbers
    printf("Enter two numbers num1 and num2 : \n");
    // Read two numbers from the user
    scanf("%d%d", &num1, &num2);

    //Calling function to add two number
    sum = addTwoNumber(num1,num2);

    // Print the sum
    printf("Addition of num1 and num2 is: %d", sum);

    return 0;
}

Output:

Enter two numbers num1 and num2:  2  3
Addition of num1 and num2 is: 5

 

 

Recommended Post for you: