Signup/Sign In

C++ Adding Two Numbers Program

Hello Everyone!

In this tutorial we will learn how to write a basic program to add two numbers, in C++ programming language.

Code:

#include<iostream>

using namespace std;

int main()
{
    //variable declaration
    int a, b;

    //variable declaration and initialization  
    int sum=0;

    //take user input
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to add 2 numbers ===== \n\n";

    cout << "Enter the first Number : ";
    cin >> a;

    cout << "\nEnter the second Number : ";
    cin >> b;

    //Adding the two numbers
    sum = a + b;

    //displaying the final output (sum)
    cout << "\nAddition of the two numbers is : " << sum;

    return 0;
}

Output:

Output for adding 2 numbers

Program to Add 2 numbers using C++ explained:

1. int sum=0;

A variable can be initialized at the time of declaration itself. This is usually done to make sure that the value to be used in future is not initially set to some random value.

2. sum = a + b;

All the basic mathematical operations such as:

  1. Addition (+)

  2. Subtraction (-)

  3. Multiplication (*)

  4. Division (/)

  5. Remainder or Modulus (%) etc.

can be performed between two numbers (at a time) in C++, similar to that in any other programming language.

We will recommend you to modify the program and test all the above operations yourself, to develop better understanding.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.