Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program to Add two Integers
A program to add two integers takes two numbers, performs their mathematical sum, and stores the result in another variable.
Syntax
int sum = number1 + number2;
Method 1: Adding Fixed Values
This approach demonstrates adding two predefined integer values −
#include <stdio.h>
int main() {
int a = 545;
int b = 123;
int sum = a + b;
printf("The first number is %d<br>", a);
printf("The second number is %d<br>", b);
printf("The sum of two numbers is %d<br>", sum);
return 0;
}
The first number is 545 The second number is 123 The sum of two numbers is 668
Method 2: Adding User Input Values
This approach takes input from the user and calculates the sum −
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first integer: ");
scanf("%d", &num1);
printf("Enter second integer: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum of %d and %d is: %d<br>", num1, num2, sum);
return 0;
}
Enter first integer: 25 Enter second integer: 35 Sum of 25 and 35 is: 60
Key Points
- The + operator performs arithmetic addition on integers
- Variables must be declared before use in C
- The
scanf()function reads user input with proper format specifiers
Conclusion
Adding two integers in C is straightforward using the + operator. Both fixed values and user input methods work effectively for mathematical addition operations.
Advertisements
