In this post, we will learn how to subtract two numbers using the C++ Programming language.
The below program asks the user to enter two numbers, after that it performs the subtraction using the minus (-) operator and stores the result in a third variable.
Then, it prints the result on the screen using the cout statement.
So, without further ado, let’s begin this tutorial.
C++ Program to Subtract Two Numbers
// C++ Program to Subtract Two Numbers #include <iostream> using namespace std; int main(){ int num1, num2, sub; // Asking for input cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number: "; cin >> num2; // Subtract two numbers sub = num1 - num2; // Display output cout << "The subtraction of two numbers = " << sub << endl; return 0; }
Output
Enter the first number: 32
Enter the second number: 18
The subtraction of two numbers = 14
How Does This Program Work?
int num1, num2, sub;
In this program, we have declared three integer data type variables named num1, num2, and sub.
// Asking for input cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number; "; cin >> num2;
The user is asked to enter two numbers. The entered numbers get stored in the num1 and num2 named variables.
// Subtract two numbers sub = num1 - num2;
After that, we perform the subtraction of two numbers using the minus (-) arithmetic operator. The result gets stored in the sub named variable.
// Display output cout << "The subtraction of two numbers = " << sub << endl;
Finally, the result is displayed on the screen using the cout statement.
Conclusion
I hope after going through this post, you understand how to subtract two numbers using the C++ Programming language.
If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.
Also Read: