C++ program to add two integer numbers using pointers

Given two integer numbers, we have to add them using pointers in C++.
[Last updated : February 28, 2023]

Adding two integer numbers using pointers

In last two programs, we have discussed how to find addition of two integer numbers with a normal way and addition of two integer numbers using user defined function?

In this program, we are calculating addition/sum of two integer numbers using pointers, here we will declare three integer pointers two store two input numbers and to store addition of the numbers.

Program to add two numbers using pointers in C++

#include <iostream>
using namespace std;

int main()
{
    int* pNum1 = new int;
    int* pNum2 = new int;
    int* pAdd = new int;

    //read numbers
    cout << "Enter first number: ";
    cin >> (*pNum1);
    cout << "Enter second number: ";
    cin >> (*pNum2);

    //calculate addition
    *pAdd = *pNum1 + *pNum2;

    //print addition
    cout << "Addition is: " << *pAdd << endl;

    return 0;
}

Output

Enter first number: 100 
Enter second number: 200
Addition is: 300

Here, we declared pointer variables using new operator, read more about it : new operator in C++



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.