C++ Program to Add Two Integers

In this post, you’ll learn how to write a Program to Add Two Integers in C++.

This lesson will help you learn how to add to two integers, one of the basic Mathematical functions using the C++ language. Let’s look at the below source code

How to Add Two Integers in C++?


Source Code

#include <iostream>
using namespace std;
int main()
    int a=2, b=4;
    int c;
    cout<<"The value of a is: "<<a;
    cout<<"\nThe value of b is: "<<b;
    c= a + b;
    cout<<"\nThe sum of a and b is: "<<c;
    return 0;


The value of a is: 2
The value of b is: 4
The sum of a and b is: 6

Let’s break down the code and elaborate the steps, to understand better

#include <iostream>

  • This line which is called the header file is used in every C++ codding. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code.
  • The <iostream> file stands for Input and Output statement. This file includes the output statement ‘cout’, ‘cin’, and various other statements.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. First declare the variables (a, b, c) as integers and assign the values to be calculated –  int a=2, b=4; int c;.
  2. Next display the output statements with the cout and the Insertion Operator ‘ << ‘.
  3. When using characters/sentences, write them within quotes ” “, "Hello World!" followed by ‘ << ‘ and the integer, to display them.
  4.  Now we can include the statement to perform the Mathematical function – Sum of two numbers. Using the addition operator and assign an integer to store the answer c = a + b;.
  5. Using the output statement cout and the integer c, display the answer.
  6. End the function with return 0; this returns the function to main( ).

Now that you have understood how the source code performs its function, try it yourself with different values to it understand better.

Note: The ‘ \n ‘ in the code is used to insert a new line, to understand how it works exclude it from the code and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured as to receive the values from the user rather than include the values directly in the code.

#include <iostream>
using namespace std;
int main()
    int a,b;
    int c;
    cout<<"Enter the value of a: "<<a;
    cout<<"\nEnter the value of b: "<<b;
    c = a + b;
    cout<<"\nSum of a " <<a<< " and " <<b<< " is: "<<c;
    return 0;




Enter the value of a: 5
Enter the value of b: 3
Sum of a 5 and 3 is: 8
  • The structure is similar to the previous code, the extra function we include is the cin>>a; and cin>>b; , this statement is used to collect and store the values that the user enters in the assigned integer.
  • We use <<a<<  <<b<<   <<c to display the stored values in the output.
  • The rest of the statements and functions are similar, to perform the Addition Operation.