codescracker


c

C Program to Add Two Numbers using Pointer



« Previous Program Next Program »


Add Two Numbers using Pointer in C

To add two numbers using pointer in C programming, you have to ask to the user to enter the two number, then make two pointer say *ptr1 and *ptr2 of same type to initialize the address of both the number and make another variable say sum which contain the addition of the two number i.e., sum = *ptr1 + *ptr2 and display the result

C Programming Code to Add Two Numbers using Pointer

Following C program ask to the user to enter the two number and add the entered two number using pointer then display the addition on the screen:

/* C Program - Add Two Numbers using Pointer */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int num1, num2, *ptr1, *ptr2, sum=0;
	printf("Enter the two number :");
	scanf("%d%d",&num1,&num2);
	ptr1 = &num1;
	ptr2 = &num2;
	sum = *ptr1 + *ptr2;
	printf("Sum of the two number is %d",sum);
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program to add two numbers using pointer

Here we initialize the address of first number to first pointer and the address of second number to second pointer. Now using value at address operator (*), we add the two number and initialize to sum and print the added number on the screen

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test