C program to add two numbers using pointers

Previous Program Next Program

Write a C program to read two numbers from user and add them using pointers. How to find sum of two number using pointers in C programming. Program to perform arithmetic operations on number using pointers.

Example

Input

Input num1: 10
Input num2: 20

Output

Sum = 30
Difference = -10
Product = 200
Quotient = 0

Required knowledge

Basic C programming, Pointers

Since the first days of writing programs in C. We have performed arithmetic operations so many times. In case you want a quick recap check out these links without pointers -

In previous post I explained how to store address of a variable in pointer variable. Let us now learn to work with the value stored in the pointer variable.

There are two common operators used with pointers -

  1. & (Address of) operator - When prefixed with any variable returns the actual memory address of that variable.
  2. * (Dereference) operator - When prefixed with any pointer variable it evaluates to the value stored at the address of a pointer variable.

Program to add two numbers using pointers

/**
 * C program to add two number using pointers
 */

#include <stdio.h>

int main()
{
    int num1, num2, sum;
    int *ptr1, *ptr2;

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

    printf("Enter any two numbers: ");
    scanf("%d%d", ptr1, ptr2);

    sum = *ptr1 + *ptr2;

    printf("Sum = %d", sum);

    return 0;
}

You have noticed that, I haven't used any & (address of) operator in the scanf() function. scanf() takes the actual memory address where the user input is to be stored. The pointer variable ptr1 and ptr2 contains the actual memory addresses of num1 and num2. Therefore we need not to prefix the & operator in scanf() function in case of pointers.

Now using this, it should be an easy workaround to compute all arithmetic operations using pointers. Let us write another program that performs all arithmetic operations using pointers.

Program to perform all arithmetic operations using pointers

/**
 * C program to perform all arithmetic operations using pointers
 */

#include <stdio.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;
}
Note Always use proper spaces and separators between two different programming elements. As *ptr1 /*ptr2 will generate a compile time error. Find out why?
Output
Enter any two real numbers: 20
5
Sum = 25.00
Difference = 15.00
Product = 100.00
Quotient = 4.00

Happy coding ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment