Programming in C (312303) Practical No. 25: Implement C programs to perform arithmetic operations using pointer

Programming in C (312303) Practical No. 25: Implement C programs to perform arithmetic operations using pointer

Programming in C (312303)
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 integers
    int *ptr1 = &num1;
    int *ptr2 = &num2;
    // Perform addition using pointers
    sum = *ptr1 + *ptr2;  // Dereference pointers to access values
    printf(“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 pointers
    ptr1 = &num1;
    ptr2 = &num2;
    // Perform subtraction using pointers
    int difference = *ptr1 – *ptr2;  // Dereference pointers to access values
    printf(“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 pointers
    printf(“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. 

Suraj Diware
Suraj Diware

Hi everyone, I'm Suraj Diware, and I'm passionate about helping students succeed in MSBTE programs. This blog is dedicated to providing clear and concise explanations of MSBTE curriculum topics, along with practical tips and resources to support your learning journey.

Articles: 640

Leave a Reply

Your email address will not be published. Required fields are marked *