Program to perform Arithmetic Operations using Pointers in C

Program-to-perform-Arithmetic-Operations-using-Pointers-in-C-language

Program Description:

Write a Program to perform all arithmetic operations using pointers in c programming language. The program will accept two integer variables from the user and calculates the addition, subtraction, multiplication, division, and modulo division using the pointers.

📢This program is part of the Pointers practice programs series.

Let’s look at the example input and output of the program.

Example Input and Output:

Input:

Enter value for first number(num1): 200

Enter value for second number(num2): 100

Output:

Sum of 200+100 = 300

Subtraction of 200-100 = 100

Product of 200*100 = 20000

Division of 200/100 = 2

Modulo Division of 200%100 = 0

The program took two integers from the user, performed all arithmetic operations, and provided the result.

Program Prerequisites:

To better understand the following program, Please go through the below Pointers and Function tutorials.

Arithmetic Operations using Pointers in C Program Explanation:

We are going to define five functions to perform arithmetic operations. They are sum(), subtract(), product(), division(), and moduloDivision() functions.

The prototype of all functions is the same, They accept three integer pointer variables as formal arguments and perform the respective operation.

For example, The subtract() function takes three integer pointers num1, num2, and result. and subtracts the num2 from the num1 variable and stores the results in the result variable.

Here is the prototype of the subtract() function.

void subtract(int *num1, int *num2, int * result)

The subtract() function dereferences the num1 and num2 pointer variables to get the values of num1 and num2.

*result = *num1 - *num2;

Similarly, All other functions also dereference and perform the respective operations on the num1 and num2

  • sum() function
    • *result = *num1 + *num2;
  • division() function
    • *result = *num1 / *num2;
  • product() function
    • *result = *num1 * *num2;
  • moduloDivision() function
    • *result = *num1 % *num2;

Finally, Call the sum(), subtract(), product(), moduloDivision(), and division() functions from the main() function and pass the num1 and num2 and result variables with addresses using the address of operator(‘&’)

Ex:

sum(&num1, &num2, &result);

Print the results on the console.

📢As we are not changing the num1 and num2 variables, We can simply pass them as normal variables as well (without addresses)

So change the above program like below.

Ex:
product(num1, num2, &result);

void product(int num1, int num2, int * result);

Program to Calculate all Arithmetic Operations using Pointers in C Language:

Here is the program to calculate all arithmetic operations using pointers in C programming language.

We have provided detailed comments for your convenience.

Arithmetic operations using pointers in C – Program Output:

Let’s compile and Run the program using your favorite Compiler.

$ gcc arith-pointer.c

Run the Program.

Test Case 1:

arithmetic-operations-using-pointers-in-c-program-output

As we can see from the above output, The user provided the num1as 200and num2 as 100. And the program performed all arithmetic operations and displayed the output on the console.

Let’s look at another example.

Test Case 2:

The program is generating the desired results.

Related C Practice Programs:

  1. C Tutorials Index
  2. C Programs Index – 300+ Programs
  3. C Program to Perform arithmetic Operations using Switch Statement
  4. C Program to Check the Sign of the Number ( Positive, Negative, or Zero) Using Switch Case
  5. C Program to find whether the alphabet is a Vowel or Consonant using Switch Case
  6. Enter the week number and print the day of week program Using Switch Statement
  7. C Program to calculate the Number of days in Month using the switch statement
  8. C Program to check Even or Odd Number using Switch Case
  9. C Program to find the maximum of two numbers using a switch case
  10. Menu Driven Calculator Program using Switch Case and Do while

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Program to perform Arithmetic Operations using Pointers in C […]

  2. […] the following program, We have defined a function called performArithOps. The function performs all arithmetic operations like addition, subtraction, product, division, and modulo division on given two integers. Here is […]

Leave a Reply