Add Two Numbers Program

Last Updated : 15 Apr, 2026

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 = 20

Input: a = 1, b = 8
Output: 9
Explanation: Sum of 1 + 8 = 9

Try It Yourself
redirect icon

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 a and b)
  • Apply the addition operator: sum = a + b
  • Output the result (sum)
C++
#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;
}
C
#include <stdio.h>

int main() {
    int a = 11, b = 9;

    // Adding the two numbers and printing there sum
    printf("%d", a + b);

    return 0;
}
Java
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);
    }
}
Python
a = 11
b = 9

# Adding the two numbers and printing there sum
print(a + b)
C#
using System;

class Program {
    static void Main() {
        int a = 11, b = 9;

        // Adding the two numbers and printing there sum
        Console.WriteLine(a + b);
    }
}
JavaScript
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 a and b
  • Repeat b times:
  • Increment a by 1
  • The final value of a is the sum
C++
#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;
}
C
#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;
}
Java
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);
    }
}
Python
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()
C#
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);
    }
}
JavaScript
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.
C++
#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;
}
C
#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;
}
Java
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);
    }
}
Python
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()
C#
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);
    }
}
JavaScript
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
Comment