Write a program to add two numbers using pointers
Question
Write a program to add two numbers using pointers
Solution
Sure, here is a simple C++ program that adds two numbers using pointers:
#include<iostream>
using namespace std;
int main() {
int num1, num2, *ptr1, *ptr2, sum;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
ptr1 = &num1; // ptr1 stores the address of num1
ptr2 = &num2; // ptr2 stores the address of num2
sum = *ptr1 + *ptr2; // add the values at the addresses stored in ptr1 and ptr2
cout << "Sum = " << sum; // display the sum
return 0;
}
In this program, we are using two pointers ptr1 and ptr2 to store the addresses of num1 and num2 respectively. Then we are adding the values at the addresses stored in ptr1 and ptr2 and storing the result in sum. Finally, we are displaying the sum.
Similar Questions
Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c
DebuggingAdd two numbers using the pointer to function fill the remaining code to perform additionoutput:60 Font SizeLanguage Editor Theme #include <stdio.h>int sum(int x, int y){ return x+y;}int main( ){ int (*fp)(int, int); fp = sum;
Roshene has two integer variables, x, and y. She wants to perform the following operations using pointers:Increment the value of x by 3.Decrement the value of y by 2.Multiply the values of x and y and store the result in x.Write a program to help Roshene achieve this and display the updated values of x and y.Input format :The input consists of two integer values x and y in separate lines.Output format :The first line of output displays "x = " followed by the updated value of x after multiplication.The second line displays "y = " followed by the decremented value of y.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ x ≤ 5001 ≤ y ≤ 450Sample test cases :Input 1 :59Output 1 :x = 56y = 7Input 2 :4512Output 2 :x = 480y = 10Input 3 :478356Output 3 :x = 170274y = 354
Using pointers write a program to swap two numbers using a user definedfunction(Call by reference)
Write a Java program to add two binary numbers.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.