pointer

C++ Example to subtract two integer using pointer

C++ Example to subtract two integer using pointer

In this article, we will discuss the concept of the C++ 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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2;      //1 declare variable
    int *ptr1,*ptr2;    //2 declare pointer variable
    int sub;              //3
    num1=3150;    //4 variable initiakization
    num2=570;

    ptr1=&num1;     
    ptr2=&num2;
 //5 assign the address of the variable to pointer variable
    sub=*ptr1 - *ptr2;                                 //6
//calculate subtraction
    cout<<"Subtraction of the given two integer values is: "<<sub;  //7
//find the output and display 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:2580

 

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 without using pointer in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

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

    cout<<"Please enter value for num1: ";    //4
    cin>>num1;                           //5
    cout<<"Please enter value for num2: ";    //4
    cin>>num2;                           //7

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

    sub=*ptr1 - *ptr2;                                 //10

    cout<<"Product of the two integer values is: "<<sub;  //11
    getch();
    return 0;
}

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

Please enter value for num1:2000
Please enter value for num2: 500
Subtraction of the given two integer values is: 1500

 

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 cin>> to store the first input value in num1
  6. Request the user to enter second input for store variable num2
  7. Using cin>> to 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 sum of num1,num2 using their addresses and add them using the pointer variables
  11. Finally, display result on the screen- the subtraction of both numbers num1,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

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

Data type in C++ language

Variable in C++ language

Operator n C++ language

Pointer in C++ language

Example to subtract two integer using pointer in C
Java Example to subtract two integer using betwise operator
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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago