C Program to add two numbers using pointer

Simple C program to add two numbers using pointer: Pointer is a variable that store the address of another variable. So to add two number, first we store the address of first number into pointer to integer p and address of second number into pointer to integer q. Sum of two numbers will be *p+*q.
 
/* C program to add two numbers using pointer */
#include<stdio.h>
int main()
{
 int first,second,*p,*q,sum;
 printf("Enter first number: ");
 scanf("%d",&first);
 printf("Enter second number: ");
 scanf("%d",&second);
 p=&first;
 q=&second;
 sum=*p+*q;
 printf("Sum= %d",sum);
 return 0;
}
/* End of the program */
 

Another Program to add two numbers using pointer
 
/* C program to add two numbers using pointer */
#include<stdio.h>
int *AddTwoNumbers(int *first,int *second)
{
 
 *first=*first+*second;
 return first;
}
int main()
{
 int first,second,*sum;
 printf("Enter first number: ");
 scanf("%d",&first);
 printf("Enter second number: ");
 scanf("%d",&second);
 sum=AddTwoNumbers(&first,&second);
 printf("Sum= %d",*sum);
 return 0;
}
/* End of the program */


Popular posts from this blog