Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Python program to subtraction of two numbers | 4 different Methods

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

Posted on August 8, 2020August 31, 2020

Table of Contents

  • C++ program to subtraction of two numbers | 6 different Methods
    •  Subtraction of two numbers – standard method
    • Subtraction of two numbers-Using user input
    • Subtraction of two numbers – using function
    • Subtraction of two numbers – using recursion
    • Subtraction of two numbers – using pointer
    • Subtraction of two numbers – without – operator

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes