Add Two Numbers

C++ Program to Add Two Numbers (6 Methods With Output)

BeginnerTopic: Basic Programs
Back

C++ Add Two Numbers Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int num1, num2;
    
    cout << "Enter first number: ";
    cin >> num1;
    
    cout << "Enter second number: ";
    cin >> num2;
    
    int sum = num1 + num2;
    
    cout << "Sum of " << num1 << " and " << num2 << " = " << sum << endl;
    
    return 0;
}
Output
Enter first number: 15
Enter second number: 27
Sum of 15 and 27 = 42

Understanding Add Two Numbers

This program demonstrates 6 different methods to add two numbers: basic addition using + operator, using functions, using pointers, using references, using classes and objects (OOP), and using template functions for generic programming.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents