➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
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
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 you find anything incorrect? Let us know in the comments. Thank you!