C Program to Add Two Numbers Using Pointers
Write a C program to Add Two Numbers Using Pointers
- Takes input of two integers from the user.
- Stores the addresses of the two integers in pointer variables ptr1 and ptr2.
- Dereferences the pointers to access values and compute their sum.
- Prints the calculated sum using the pointer-referenced values.
Example : pgm.c
#include <stdio.h> int main() { int num1, num2, sum; int *ptr1, *ptr2; // Input two numbers printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Assign addresses to pointers ptr1 = &num1; ptr2 = &num2; // Calculate sum using pointers sum = *ptr1 + *ptr2; // Output the result printf("Sum = %d\n", sum); return 0; }
Output :
Enter first number: 10
Enter second number: 20
Sum = 30
Enter second number: 20
Sum = 30