Add Two number Using Pointer in C++


Q. Write a C++ program to add two numbers using pointers.

#include <iostream>       
using namespace std;
  
int main()
{  
    int i, j, *a, *b, sum;
    cout << "Enter two integers to add: ";
    cin >> i >> j ;
    a = &i;
    b = &j;
    sum = *a + *b;
    cout << "Addition of the numbers is :" << sum;
    return 0;
}

Q. Write an algorithm to add two numbers using pointers.

 1. Start
2. Declare three integer variables: `i`, `j`, and `sum`.
3. Declare two integer pointers: `a` and `b`.
4. Prompt the user to enter two integers.
5. Read the two integers from user input into variables `i` and `j`.
6. Assign pointer `a` to the address of `i`.
7. Assign pointer `b` to the address of `j`.
8. Calculate the sum by dereferencing pointers `a` and `b` and adding their values.
- `sum = *a + *b`
9. Display the result of the addition.
10. End



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.