C++ Program to Add Two Numbers using Pointer

« Previous Program Next Program »

Add two Numbers using Pointer in C++

To add two numbers using pointer in C++ Programming, you have to ask to the user to enter the two number, then make two pointer type variable of same type say *ptr1 and *ptr2 to initialize the address of both the number and make another variable say sum which contain the addition of the two number like sum = *ptr1 + *ptr2 and display the result on the screen.

Here * is also called as value at address operator.

C++ Programming Code to Add Two Numbers using Pointer

Following C++ program ask to the user to enter the two number and add the entered two number using pointer then display the addition result on the screen:

/* C++ Program - Add Two Numbers using Pointer */
void main()
	int num1, num2, *ptr1, *ptr2, sum=0;
	cout<<"Enter the two number :";
	ptr1 = &num1;
	ptr2 = &num2;
	sum = *ptr1 + *ptr2;
	cout<<"Sum of the two number is "<<sum;

When the above C++ program is compile and executed, it will produce the following result:

C++ program to add two numbers using pointer

Here we initialize the address of first number to first pointer and the address of second number to second pointer.

Now using value at address operator (*), we add the two number and initialize to sum and print the added number on the screen

Same Program in Other Language

You may like the same program in other programming languages:

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test