Programming in C (312303) Practical No. 25: Implement C programs to perform arithmetic operations using pointer
Programming in C (312303) Practical No. 25 |
In this Programming in C (312303) Practical No. 25 Students will be able to understand arithmetic operations that can be performed on pointers. Students will be able to write a c program to perform arithmetic operations on pointers.
Exercise
1. Addition using pointer
Answer:
#include <stdio.h>int main() {int num1 = 10, num2 = 20;int sum;// Declare pointers to integersint *ptr1 = &num1;int *ptr2 = &num2;// Perform addition using pointerssum = *ptr1 + *ptr2; // Dereference pointers to access valuesprintf(“The sum of %d and %d is %dn”, num1, num2, sum);return 0;}
Output:
The sum of 10 and 20 is 30
=== Code Execution Successful ===
2. Subtraction using pointer
Answer:
#include <stdio.h>int main() {int num1 = 100, num2 = 50;int *ptr1, *ptr2;// Assign addresses of num1 and num2 to the pointersptr1 = &num1;ptr2 = &num2;// Perform subtraction using pointersint difference = *ptr1 – *ptr2; // Dereference pointers to access valuesprintf(“Difference: %dn”, difference);return 0;}
Output:
Difference: 50
=== Code Execution Successful ===
3. Multiplication using pointer
Answer:
#include <stdio.h>int main() {int num1 = 10, num2 = 5;int *ptr1 = &num1, *ptr2 = &num2;int result = *ptr1 * *ptr2; // Multiply values pointed to by pointersprintf(“Multiplication result: %dn”, result);return 0;}
Output:
Multiplication result: 50
=== Code Execution Successful ===
Practical Related Questions
1. Assume that float takes 4 bytes. Predict the output of the following program.
#include<stdio.h>int main (){float arr[S]={12.5,10.0,13.5,90.5,0.5};float *ptrl =&arr[0];float *ptr2= ptr+1;printf(“%fn”, *ptr2);printf(” %d”, ptrl-ptr2);return 0;}
Output:
10.0
1
Conclusion
We successfully completed Practical No. 25 of the Programming in C (312303) manual solved all practical related questions and implemented c codes.