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:
- find the sum of natural numbers up to n terms using C program
- Get the sum of even natural numbers from 1 to n using C
- C Program to find the sum of odd natural numbers from 1 to n
- C Program to find if the given number is the sum of first n natural numbers
- C program to find the sum of first and last digit of a Number
- C program to find the sum of digits of a number
- C Program to find Armstrong numbers
- C Program to find nth Armstrong numbers
- C Program to find neon number
- C Program to check if a number is positive, negative or zero using bit operators
- C Program to check positive or negative without using conditional statements