C program for addition of two numbers
In this post, you will learn the addition of two numbers using C program. Such a type of basic program is useful to improve your logic building.
C Program to find sum of two numbers
In the given program, we have asked the user to enter two integers. Then, we have calculated the sum of these two integers using the + operator and displayed it on the screen-
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// calculating sum
sum = num1 + num2;
printf("%d + %d = %d", num1, num2, sum);
return 0;
}
Output of the above code -
Enter first number: 52
Enter second number: 245
52 + 245 = 297
C Program to add two numbers using a function
Here, we have created a user-defined function sum() and written the addition logic. We are called this function from the main function.
#include <stdio.h>
// Define function
int sum(int a, int b){
return a+b;
}
int main()
{
int num1, num2, total;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
//Calling the function
total = sum(num1, num2);
printf("Sum of the entered numbers: %d", total);
return 0;
}
Output
Enter first number: 52
Enter second number: 64
Sum of the entered numbers: 116
C Program to add two real numbers
In the given example, we add two real numbers using C programming, that's why we use double data type.
#include <stdio.h>
int main()
{
double a, b, sum;
printf("Enter two numbers:\n");
scanf("%lf%lf", &a, &b);
sum = a + b;
printf("%lf\n", sum);
return 0;
}
Output of the above code:
Enter two numbers:
23.22 53.23
76.450000
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative