C++ Program to Add Two Numbers


Example: Program to Add Two Integers

#include <iostream>
using namespace std;

int main() {

  int first_number, second_number, sum;
    
  cout << "Enter two integers: ";
  cin >> first_number >> second_number;

  // sum of two numbers in stored in variable sumOfTwoNumbers
  sum = first_number + second_number;

  // prints sum 
  cout << first_number << " + " <<  second_number << " = " << sum;     

  return 0;
}

Output

Enter two integers: 4
5
4 + 5 = 9

In this program, the user is asked to enter two integers. These two integers are stored in variables first_number and second_number respectively.

Then, the variables are added using the + operator and stored in the sum variable.

Finally, sum is displayed on the screen.


Also Read:

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to add two numbers.

  • Return the sum of num1 and num2.
  • For example, if num1 = 4 and num2 = 5, the return value should be 9.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges