Subtract two numbers
Table of Contents
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
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,
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,
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
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
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
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
Suggested post
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.