Calculations

Example to subtract two integer using pointer in C

Example to subtract two integer using pointer in C

In this article, we will discuss the concept of the Example to subtract two integer using pointer in C programming language

In this post, we are going to learn how to  write a program to find the subtraction of two numbers using pointer in C programming language

Subtract two integer using pointer

Code to find the subtraction of  two numbers 

Subtract two integer using pointer

The program use to calculate subtraction of given two integer numbers using pointer in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2;     //declare variables num1,num2
    int *ptr1,*ptr2;   //declare pointer variable 
    int sub;             
    num1=350;   //variable initialization
    num2=50;

    ptr1=&num1;      //assign the address of the variable to pointer variable
    ptr2=&num2;

    sub=*ptr1 - *ptr2;                                 
//calculate the subtraction using pointer
    printf("Subtraction of the given two integer values is: %d",sub); 
//display result on the screen
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Subtraction of the given two integer values is: 300

 

Subtract two integer using pointer – takes input from the user

The program allow the user to enter two numbers and then calculates subtraction of given two integer numbers  using pointer in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2;      //1
    int *ptr1,*ptr2;    //2
    int sub;              //3

    printf("Please enter value for num1: ");    //4
    scanf("%d",&num1);                           //5
    printf("Please enter value for num2: ");      //6
    scanf("%d",&num2);                             //7

    ptr1=&num1;                                      //8
    ptr2=&num2;

    sub=*ptr1 - *ptr2;                                 //10

    printf("Subtraction of the two integer values is: %d",mul);  //11
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Please enter value for num1: 1000
Please enter value for num1: 700
Subtraction of the two integer values is: 300

 

Methods

  1. Create two variables to store two numbers as input provided by the user: num1,num2;
  2. Create two pointer variables (*ptr1,*ptr2)to store the address of the numbers: num 1 and num2
  3. Create a variable to store the subtraction of these numbers: sub
  4. Request the user to enter first input for store variable num1
  5. Using scanf() function store the first input value in num1
  6. Request the user to enter second input for store variable num2
  7. Using scanf() function store the second input value in num2
  8. Assign the address of variable num1 to ptr1
  9. Assign the address of variable num2 to ptr2
  10. Find the subtraction of two number using their addresses – find the subtraction of num1,num2 using their addresses and add them using the pointer variables
  11. Finally, display result on the screen- the sum of both numbers num1 and num2

 

Similar post

C code to add two numbers

C program find the sum of two numbers using the function

C program find the sum of two numbers using recursion

C program to find the product of two numbers using the pointer

 

 

Suggested for you

Input-output function in C language

Data type in C language

Variable in C language

Operator n C language

Pointer in C language

 

 

C++ Example to subtract two integer without using minus operator
C++ Example to subtract two integer using pointer
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago