C++

C++ program to subtraction of two numbers | 6 different Methods

C++ program to subtraction of two numbers | 6 different Methods

In this tutorial, we will discuss the C++ program to subtraction of two numbers

In this post, we are going to learn how to find subtraction of two numbers via different 6 ways

Subtraction of two numbers – standard method

Program 1

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

int main()
{
    int num1,num2,sub;//variable declaration
    num1=567;
    num2=321;

sub=num1-num2;//calculate the subtraction
    cout<<"subtraction of "<<num1<<" and "<<num2<<" :"<<sub;
    //display the result of subtraction
    getch();
    return 0;
}

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

subtraction of 567 and 321 : 246

 

In this program,

  • Integer variable num1 and num2 both are declared and initialized
  • The num1 and num2 both are subtracted together and the value assigned to the integer variable sub
  • Finally the program is displayed the output using cout<<

 

Subtraction of two numbers-Using user input

Program 2

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

int main()
{
    int num1,num2,sub;//variable declaration
    cout<<"Enter the first number: ";
    cin>>num1;//read value for num1
    cout<<"Enter the second number: ";
    cin>>num2;//read value for num2

sub=num1-num2;//calculate the subtraction
    cout<<"subtraction of "<<num1<<" and "<<num2<<" :"<<sub;
    //display the result of subtraction
    getch();
    return 0;
}

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

Enter the first number: 987
Enter the first number: 789
Subtraction of 987 and 789 : 198

 

in tis program,

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2.
  • The num1 and num2 both are subtracted together and the value assigned to the integer variable sub
  • Finally the program is displayed the output using cout<<

 

Subtraction of two numbers – using function

Program 3

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

int subNum(int a, int b);//Function prototype
int main()
{
    int result,num1,num2;
    cout<<"Enter two numbers to subtract\n";
    cin>>num1>>num2;

    result=subNum(num1,num2);
     cout<<"subtraction of given two numbers:"<<result;
     getch();
    return 0;
}
int subNum(int x, int y)
{
    int c=x-y;
    return (c);
}

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

Enter two numbers to subtract
1000
345
subtraction of given two numbers: 655

 

  • Declare a function named as sub() with two int parameters
  • Declare num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Define the function (sub())for find sum
  • Call the function to produce output
  • Finally, the program displays the result of subtraction using cout<<

 

Subtraction of two numbers – using recursion

Program 4

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

int subNum(int a, int b);//Function prototype
int main()
{
    int result,num1,num2;
    cout<<"Enter two numbers to subtract\n";
    cin>>num1>>num2;

    result=subNum(num1,num2);
     cout<<"subtraction of given two numbers:"<<result;
     getch();
    return 0;
}
int subNum(int num1, int num2)
{
    if(num2==0)
        return num1;
    else
        return subNum((num1-1),(num2-1));
}

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

Enter two numbers to subtract
1200
450
subtraction of given two numbers: 750

 

  • Declare a function named as sub() with two int parameters
  • Declare num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Define the function (sub())for find subtraction recursively
  • Call the function to produce output
  • Finally, the program displays the result of subtraction using cout<<

 

Subtraction of two numbers – using pointer

Program 5

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

int subNum(int *a, int *b);//Function prototype
int main()
{
    int result,num1,num2;
    cout<<"Enter two numbers to subtract\n";
    cin>>num1>>num2;

    result=subNum(&num1,&num2);
     cout<<"subtraction of given two numbers:"<<result;
     getch();
    return 0;
}
int subNum(int *a, int *b)
{
    int temp;
    temp=*a-*b;
    return temp;
}

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

Enter two numbers to subtract
1234
123
subtraction of given two numbers: 1111

 

  • Declare variables num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Define the function (sub()) for find subtraction
  • Pass the parameter to the function as pointer variable(int *a, int *b)
  • Call the function to produce output
  • Assign the value to result variable
  • Finally, the program displays the result of subtraction using cout<<

 

Subtraction of two numbers – without – operator

Program 6

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

int main()
{
    int num1,num2,sub;//variale declaration

    //input first number
    cout<<"Enter the first number\n";
    cin>>num1;//read first number

    //input second number
    cout<<"Enter the second number\n";
    cin>>num2;//read second number

    sub=num1+~num2+1;//calculate the subtraction
     cout<<"subtraction of"<<num1<<"-"<<num2<<":"<<sub;
     //display the result
getch();
    return 0;
}

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

Enter the first number
678
Enter the second number
234
subtraction of 678-234: 444

 

  • Declare variables num1,num2 and sub.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Calculate the subtraction of num1,num2
  • Finally, the program displays the result of subtraction using cout<<

 

Suggested post

Operator in C++ language

 

Similar post

C program to subtraction of two numbers | 6 different Methods

C++ program to subtraction of two numbers | 6 different Methods

Java program to subtraction of two numbers | 5 different Methods

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

6 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

6 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

6 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

6 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

7 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

7 months ago

This website uses cookies.