Write a program in C to add two numbers using pointers

Previous
Next

Solution:

  • We read the input values into variables n1, n2 using pointer variables p1 and p2.
  • We add the values of n1 and n2 through pointers.
#include <stdio.h>
int main()
{
	int n1, n2, sum;
	int *p1, *p2;
	p1 = &n1; 
	p2 = &n2; 
	printf("Enter any two numbers: \n");
	scanf("%d%d", p1, p2);
	sum = *p1 + *p2;
	printf("Sum = %d \n", sum);
	return 0;
}
Previous
Next

Courses Enquiry Form