C++ program to add two numbers using pointer
- Home
- Find elements
- C++ program to add two numbers using pointer
- On
- By
- 0 Comment
- Categories: Find elements, pointers
C++ program to add two numbers using pointer
C++ program to add two numbers using pointer
In this tutorial, we will discuss a concept of C++ program to add two numbers using pointer
In the C++ programming language, we can use the pointer variable to add two numbers
In this article, we are going to learn how to Display the sum of two numbers using pointer in Cpp language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2; //1 int *ptr1,*ptr2; //2 int sum; //3 cout << "Please enter value to num1: "<< endl; //4 cin>>num1; //5 cout << "Please enter value to num2: "<< endl; //6 cin>>num2; //7 ptr1=&num1; //8 ptr2=&num2; sum=*ptr1+*ptr2; //10 cout<<"Sum of the two integer values is: "<<sum; //11 getch(); return 0; return 0; }
When the above code is executed, it produces the following results
Please enter the value to num1: 36 Please enter the value to num2: 44 Sum of two integer values is: 80
Methods
Methods
- Create two variables to store two numbers as input (num1 and num2) provided by the user.
- Create two pointer variables to store the address of the numbers: num 1 and num2
- Create a variable to store the sum of these numbers: sum.
- Request the user to enter the first input to store variable num1
- Using scanf() function, store the first input value in num1
- Request the user to enter the second number to store as variable num2
- Using scanf() function, store the second input value in num2.
- Assign the address of variable num1 to ptr1
- Assign the address of variable num2 to ptr2
- Find the addition of two number using their addresses – find the sum of num1 and num2 using their addresses or add them using the pointer variables.
- Finally, display result on the screen- the sum of both numbers num1 and num2
Similar post
C++ program to add two numbers
C program to add two numbers using pointer
Suggested for you