Given two integers, the task is to add these integer number and print their sum in C++.
Examples
Input: a = 11, b = 9
Output: 20
Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8
Output: 9
Explanation: Sum of 1 + 8 = 9
Using Addition Operator
To add two numbers, use the addition operator (+). It combines the given values and produces their sum as the result.
- Take two numbers as input (say
aandb) - Apply the addition operator:
sum = a + b - Output the result (
sum)
#include <iostream>
using namespace std;
int main() {
int a = 11, b = 9;
// Adding the two numbers and printing there sum
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
Using Increment Operator (++)
Two numbers can also be added by using the increment operation. This is done by repeatedly increasing one value by 1, as many times as specified by the other value, typically using a loop.
- Take two numbers
aandb - Repeat
btimes: - Increment
aby 1 - The final value of
ais the sum
#include <iostream>
using namespace std;
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--;
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
Using Bitwise Operators
According to the Half Adder logic, sum of two bits can be obtained by using Bitwise XOR(^) and carry bit can be obtained by performing Bitwise AND(&) of two bits. We can extend this logic to integers that contains multiple bits.
- Read two integers from the user.
- Use the bitwise XOR (^) operator to add the numbers without considering the carry.
- Use the bitwise AND (&) operator to calculate the carry.
- Left shift the carry by one position to align it for the next bit addition.
- Repeat the process until there is no carry left.
#include <iostream>
using namespace std;
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;
}
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