- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program to Add Two Numbers - Using + Operator & Bitwise Addition
Add Two Numbers Using + Operator
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
int sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Enter two integers: 5 3
Sum = 8
Using the + operator is the simplest and most common way to add two integers in C. After reading the inputs with scanf(), the sum is stored in a third variable and printed.
Add Two Numbers Using Bitwise Operators
#include <stdio.h>
int add(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", add(a, b));
return 0;
}
Enter two integers: 7 5
Sum = 12
Bitwise addition repeatedly computes the carry and partial sum. The carry is calculated using bitwise AND, and the partial sum is obtained using XOR. The process continues until there is no carry left, resulting in the final sum.
⬅ Previous TopicC Program to Print an Integer Entered by the User
Next Topic ⮕C Program to Multiply Two Floating‑Point Numbers
Next Topic ⮕C Program to Multiply Two Floating‑Point Numbers
Comments
Loading comments...