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

In this post, we will develop the C++ program to add subtract multiply and divide two numbers. We will solve this problem in three ways,
1) Simple C++ program for addition, subtraction, multiplication, and division operations
2) C++ program for addition, subtraction, multiplication, and division using the function
3) C++ program for addition, subtraction, multiplication, and division using class and object

First, we will develop a Simple C++ program to solve this problem, then we will use the function concept, and later we will use class and object concept to perform addition, subtraction, multiplication, and division operations.

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

let us begin with a simple C++ program to add subtract multiply and divide two numbers.

#include<iostream>
using namespace std;
int main()
{
  // declare variables
  double num1, num2;

  // take input from end-user
  cout << "Enter two Numbers :: ";
  cin >> num1 >> num2;

  // addition of two number
  cout << num1 << "+" << num2 << " = "<< num1+num2 << endl;

  // subtraction of two number
  cout << num1 << "-" << num2 << " = "<< num1-num2 << endl;

  // multiplication of two number
  cout << num1 << "*" << num2 << " = "<< num1*num2 << endl;

  // division of two number
  cout << num1 << "/" << num2 << " = "<< num1/num2 << endl;

  return 0;
}

Output:-

Enter two Number: 20 5
20+5 = 25
20-5 = 15
20*5 = 100
20/5 = 4

In this C++ Program to add subtract divide and multiply two numbers, we define two variables num1 and num2 to store the data entered by the user. Sum, subtraction, multiplication and division are calculated as num1 + num2, num1 – num2, num1 * num2 and num1 / num2 respectively.

The modulus operator (%) does not work with float data types. So, we can’t write num1 % num2. To find the remainder, data types shouldn’t be the float.

C++ Program for Addition, Subtraction, Multiplication, and Division Using the Function

A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from main function in C++ programming.

#include<iostream>
using namespace std;

// function declaration
double add(double n1, double n2);
double subtract(double n1, double n2);
double multiply(double n1, double n2);
double divide(double n1, double n2);

// main function
int main()
{
  // declare variables
  double num1, num2;

  // take input from end-user
  cout << "Enter two Numbers :: ";
  cin >> num1 >> num2;

  // addition of two number
  cout << "Addition = "<< add(num1, num2) << endl;

  // subtraction of two number
  cout << "Subtraction = "<< subtract(num1, num2) << endl;

  // multiplication of two number
  cout << "Multiplication = "<< multiply(num1, num2) << endl;

  // division of two number
  cout << "Division = "<< divide(num1, num2) << endl;

  return 0;
}

// function to add two numbers
double add(double n1, double n2) 
{
    return n1+n2;
}

// function to subtract two numbers
double subtract(double n1, double n2) 
{
    return n1-n2;
}

// function to multiply two numbers
double multiply(double n1, double n2) 
{
    return n1*n2;
}

// function to divide two numbers
double divide(double n1, double n2) 
{
    return n1/n2;
}

Output:-

Enter two Numbers :: 45.2 5.3
Addition = 50.5
Subtraction = 39.9
Multiplication = 239.56
Division = 8.5283

C++ Program for Addition, Subtraction, Multiplication, and Division Using Class and Object

#include<iostream>
using namespace std;

class Arithmetic {
public:
    // function to add two numbers
    double add(double n1, double n2) 
    {
       return n1+n2;
    }

    // function to subtract two numbers
    double subtract(double n1, double n2) 
    {
        return n1-n2;
    }

    // function to multiply two numbers
    double multiply(double n1, double n2) 
    {
        return n1*n2;
    }

    // function to divide two numbers
    double divide(double n1, double n2) 
    {
        return n1/n2;
    }
};

int main()
{
  // declare variables
  double num1, num2;
  
  // declare object of Arithmetic class
  Arithmetic ar;

  // take input from end-user
  cout << "Enter two Numbers :: ";
  cin >> num1 >> num2;
  
  // addition of two number
  cout << "Addition = "<< ar.add(num1, num2) << endl;

  // subtraction of two number
  cout << "Subtraction = "<< ar.subtract(num1, num2) << endl;

  // multiplication of two number
  cout << "Multiplication = "<< ar.multiply(num1, num2) << endl;

  // division of two number
  cout << "Division = "<< ar.divide(num1, num2) << endl;

  return 0;
}

Output:-

Enter two Numbers :: 10 5
Addition = 15
Subtraction = 5
Multiplication = 50
Division = 2

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

3 thoughts on “C++ Program to Add Subtract Divide Multiply of Two Numbers”

  1. How about this one? Average of Quiz: Score obtained divided by total no. of items. The Quotient will be multiplied by fifty and the product will then added to fifty. General Average of Quizzes: Add the average of the three quizzes and the sum will be divided by the total no. of quizzes.

    1. In that case this post can help you:- Calculator Program in C++. It is an example of a C++ program, which asks two numbers and operators from the end-user and displays the result. The second program will keep executing until the user does not choose the exit option.

Leave a Comment

Your email address will not be published. Required fields are marked *