C Program to perform all arithmetic operations using pointers

#include <stdio.h>
#include<conio.h>
int main()
{
     float num1, num2;    // Normal variables
     float *ptr1, *ptr2;  // Pointer variables

     float sum, diff, mult, div;

     ptr1 = &num1; // ptr1 stores the address of num1
     ptr2 = &num2; // ptr2 stores the address of num2

     /* User input through pointer */
     printf("Enter any two real numbers: ");
     scanf("%f%f", ptr1, ptr2);

     /* Perform arithmetic operation */
     sum  = (*ptr1) + (*ptr2);
     diff = (*ptr1) - (*ptr2);
     mult = (*ptr1) * (*ptr2);
     div  = (*ptr1) / (*ptr2);

     /* Print the results */
     printf("Sum = %.2f\n", sum);
     printf("Difference = %.2f\n", diff);
     printf("Product = %.2f\n", mult);
     printf("Quotient = %.2f\n", div);

     return 0;
     getch();
}

Run:

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...