Program to add, subtract, multiply and divide two numbers

Trial

 Write a program to add, subtract, multiply and divide two numbers using pointers and print the addresses of all the variables.


#include <stdio.h> int main() { int num1, num2, sum, difference, product, quotient; int * ptr_num1, * ptr_num2, * ptr_sum, * ptr_difference, * ptr_product, * ptr_quotient; // Assign addresses of variables to pointers ptr_num1 = & num1; ptr_num2 = & num2; ptr_sum = & sum; ptr_difference = & difference; ptr_product = & product; ptr_quotient = & quotient; // Input two numbers printf("Enter first number: "); scanf("%d", ptr_num1); printf("Enter second number: "); scanf("%d", ptr_num2); // Print addresses of all variables printf("\nAddresses of variables:\n"); printf("Address of num1: %p\n", ptr_num1); printf("Address of num2: %p\n", ptr_num2); printf("Address of sum: %p\n", ptr_sum); printf("Address of difference: %p\n", ptr_difference); printf("Address of product: %p\n", ptr_product); printf("Address of quotient: %p\n", ptr_quotient); // Perform arithmetic operations using pointers * ptr_sum = * ptr_num1 + * ptr_num2; * ptr_difference = * ptr_num1 - * ptr_num2; * ptr_product = * ptr_num1 * * ptr_num2; if ( * ptr_num2 != 0) * ptr_quotient = * ptr_num1 / * ptr_num2; // Print results printf("\nResults:\n"); printf("Sum: %d\n", * ptr_sum); printf("Difference: %d\n", * ptr_difference); printf("Product: %d\n", * ptr_product); if ( * ptr_num2 != 0) printf("Quotient: %d\n", * ptr_quotient); else printf("Error! Division by zero is not allowed.\n"); return 0; }

إرسال تعليق