#4 | Pointers | Write a program to add two numbers using pointers. | C Language
YouTube Video :-
Question :-
Write a Program to add two numbers using Pointers
Code :-
#include<stdio.h>
void main()
{
// declaring Variables
int a,b;
int *p,*p1;
// Initilize Values
printf("Enter First Number : ");
scanf("%d",&a);
printf("\nEnter Second Number : ");
scanf("%d",&b);
// assign address of two varriables to the pointer variable
p = &a;
p1 = &b;
// sum of two numbers
printf("\nSum = %d",*p + *p1);
}
Description :-
- In this Program, we have to do summation of Two numbers , Without the use of variable that has the value.
- We have to use their memory Address and with the use of address we will find that which value is stored at that memory address.
- First we have to declare two types of Variables :-
- First is Normal Variable
- Second is pointer variable
- We Have to Declare Two Normal Int variables and two pointer variables
- So that we can store the two numbers and those numbers Memory Addresses.
- Then We have to assign the values or Numbers to Both Integer variables.
- After that we will assign their memory addresses to the both Pointer variables, With the use of Ampersand (&) Symbol.
- With the use of star (*) Sign we can Find the value that is stored in assigned memory addresses.
- Ex. pointer variable p = 366567 (Memory address of variable a that has value as 5)
- if we put * before pointer variable p ,then it will find the value that is stored in memory address 366567 that is 5
- so that the output of *p is 5
- In printf , we will do arithmetic operation like,
- *p + *p1
- Here *p will find that which value is stored in pointer variable p's memory address.
- And *p1 will find that which value is stored in pointer variable p1's memory address.
- So the output of *p + *p1 is, the value that is stored in p address and the value that is stored in p1 address.
Comments
Post a Comment