C++ Program to Add Subtract Multiply and Divide Two Numbers

Howdy readers, today you will learn how to write a C++ program to add, subtract, multiply and divide two numbers.

This tutorial prompts the user to enter two numbers, and then performs the addition, subtraction, multiplication and division on those two numbers using the arithmetic operators (+, -, -, /).

So, without any delay, let’s begin this tutorial.

C++ Program to Add Subtract Multiply and Divide Two Numbers

C++ Program

// C++ Program to Add Subtract Multiply and Divide Two Numbers
#include <iostream>
using namespace std;

int main(){
    int num1, num2;
    float sum, diff, product, div, modulus;
    
    // Taking input
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    
    // Addition of two numbers
    sum = num1 + num2;
    
    // Subtraction of two numbers
    diff = num1 - num2;
    
    // Multiplication of two numbers
    product = num1 * num2;
    
    // Division of two numbers
    div = num1 / num2;
    
    // Modulus of two numbers
    modulus = num1 % num2;
    
    // Display result
    cout << "The sum of the two numbers is: " << sum << endl;
    cout << "The difference of two numbers is: " << diff << endl;
    cout << "The product of two numbers is: " << product << endl;
    cout << "The division of two numbers is: " << div << endl;
    cout << "The modulus of two numbers is: " << modulus << endl;

    return 0;
 }

Output

Enter the first number: 12
Enter the second number: 8
The Sum of the two numbers is: 20
The difference of two numbers is: 4
The product of two numbers is: 96
The division of two numbers is: 1
The modulus of two numbers is: 4
C++ Program to Add Subtract Multiply and Divide Two Numbers

Explanation

    int num1, num2;
    float sum, diff, product, div, modulus;

We have declared two int data type variables and five float data type variables named num1, num2, sum, diff, product, div, and modulus respectively.

     cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;

The input is taken from the user using the cin statement. The entered integers get stored in the num1 and num2 named variables.

    sum = num1 + num2;

The Addition of two numbers is performed using the (+) operator. The sum of two numbers gets stored in the sum named variable.

    diff = num1 - num2;

The difference between two numbers is calculated using the (-) operator. The difference between the two numbers gets stored in the diff named variable.

    product = num1 * num2;

The product of two numbers is computed using the (*) operator. The result gets stored in the product named variable.

    div = num1 / num2;

The (/) division operator divides one number by another and returns the quotient. The quotient obtained gets stored in the div named variable.

    modulus = num1 % num2;

The (%) modulus operator returns the remainder. The remainder obtained using the modulus operator gets stored in the modulus named variable.

    cout << "Sum of two numbers is: " << sum << endl;
    cout << "Difference of two numbers is: " << diff << endl;
    cout << "Product of two numbers is: " << product << endl;
    cout << "Division of two numbers is: " << div << endl;
    cout << "Modulus of two numbers is: " << modulus << endl;

Finally, the sum, difference, product, division and modulus of two numbers are printed on the screen using the cout statement.

Conclusion

Today you learned how to write a C++ program to add, subtract, multiply and divide two numbers.

If you have any queries related to the tutorial, comment down your questions in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment