Adding two numbers is one of the most basic operations in C++. It is commonly used to demonstrate arithmetic operators and different approaches to performing addition.
- Uses arithmetic and bitwise operations to compute the sum of two integers.
- Demonstrates multiple techniques for adding numbers in C++.
- Suitable for understanding basic programming logic.
Example: The simplest way to add two numbers is by using the addition operator (+).
#include <iostream>
using namespace std;
int main() {
int a = 11, b = 9;
cout << a + b;
return 0;
}
#include <stdio.h>
int main() {
int a = 11, b = 9;
// Adding the two numbers and printing there sum
printf("%d", a + b);
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 11, b = 9;
// Adding the two numbers and printing there sum
System.out.println(a + b);
}
}
a = 11
b = 9
# Adding the two numbers and printing there sum
print(a + b)
using System;
class Program {
static void Main() {
int a = 11, b = 9;
// Adding the two numbers and printing there sum
Console.WriteLine(a + b);
}
}
let a = 11, b = 9;
// Adding the two numbers and printing there sum
console.log(a + b);
Output
20
Explanation: The + operator adds the values of a and b and displays the result.
Different Methods to Add Two Numbers
Add Two Numbers Using the Increment Operator (++)
Two numbers can also be added by repeatedly incrementing or decrementing one number based on the value of the other.
#include <iostream>
using namespace std;
int main() {
int a = 11, b = 9;
for (int i = 0; i < b; i++)
a++;
for (int i = 0; i > b; i--)
a--;
cout << a;
return 0;
}
#include <stdio.h>
int main() {
int a = 11, b = 9;
// If b is positive, increment a to b times
for (int i = 0; i < b; i++)
a++;
// If b is negative, decrement a to |b| times
for (int i = 0; i > b; i--)
a--;
printf("%d", a);
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 11, b = 9;
// If b is positive, increment a to b times
for (int i = 0; i < b; i++)
a++;
// If b is negative, decrement a to |b| times
for (int i = 0; i > b; i--)
a--;
System.out.println(a);
}
}
def main():
a = 11
b = 9
# If b is positive, increment a to b times
for i in range(b):
a += 1
# If b is negative, decrement a to |b| times
for i in range(-b):
a -= 1
print(a)
if __name__ == "__main__":
main()
using System;
class Program {
static void Main() {
int a = 11, b = 9;
// If b is positive, increment a to b times
for (int i = 0; i < b; i++)
a++;
// If b is negative, decrement a to |b| times
for (int i = 0; i > b; i--)
a--;
Console.WriteLine(a);
}
}
function main() {
let a = 11, b = 9;
// If b is positive, increment a to b times
for (let i = 0; i < b; i++)
a++;
// If b is negative, decrement a to |b| times
for (let i = 0; i > b; i--)
a--;
console.log(a);
}
main();
Output
20
Explanation: If b is positive, a is incremented b times. If b is negative, a is decremented |b| times.
Add Two Numbers Using Bitwise Operators
Addition can also be performed using bitwise operations without using the + operator.
Working
- XOR (^) computes the sum of bits without considering the carry.
- AND (&) computes the carry bits.
- The carry is left shifted by one position.
- The process is repeated until no carry remains.
#include <iostream>
using namespace std;
int main() {
int a = 11, b = 9;
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
cout << a;
return 0;
}
#include <stdio.h>
int main() {
int a = 11, b = 9, carry;
while (b) {
// Carry is AND of a and b
carry = a & b;
// Sum without carry is XOR of a and b
a = a ^ b;
// Carry is shifted by one so that it can be
// added in the next iteration
b = carry << 1;
}
printf("%d", a);
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 11, b = 9, carry;
while (b!= 0) {
// Carry is AND of a and b
carry = a & b;
// Sum without carry is XOR of a and b
a = a ^ b;
// Carry is shifted by one so that it can be
// added in the next iteration
b = carry << 1;
}
System.out.println(a);
}
}
def main():
a = 11
b = 9
carry = 0
while b!= 0:
# Carry is AND of a and b
carry = a & b
# Sum without carry is XOR of a and b
a = a ^ b
# Carry is shifted by one so that it can be
# added in the next iteration
b = carry << 1
print(a)
if __name__ == "__main__":
main()
using System;
class Program {
static void Main() {
int a = 11, b = 9, carry;
while (b!= 0) {
// Carry is AND of a and b
carry = a & b;
// Sum without carry is XOR of a and b
a = a ^ b;
// Carry is shifted by one so that it can be
// added in the next iteration
b = carry << 1;
}
Console.WriteLine(a);
}
}
function main() {
let a = 11, b = 9, carry;
while (b!= 0) {
// Carry is AND of a and b
carry = a & b;
// Sum without carry is XOR of a and b
a = a ^ b;
// Carry is shifted by one so that it can be
// added in the next iteration
b = carry << 1;
}
console.log(a);
}
main();
Output
20
Explanation: The algorithm repeatedly computes the sum without carry using XOR and the carry using AND until the carry becomes zero.