Sum Difference Multiply Divide in C++
Q. Write a C++ program for addition, subtraction, multiplication and division.
#include <iostream>
using namespace std;
/* Sum */
int sum(int num1, int num2) {
int sum=num1+num2;
return sum;
}
/* Difference */
int subtract(int num1, int num2) {
int sub=num1-num2;
return sub;
}
/* Multiplication */
int multiply(int num1, int num2) {
int mul=num1*num2;
return mul;
}
/* Division */
float divide(float num1, float num2) {
float divi=num1/num2;
return divi;
}
int main()
{
int a = 101,b=2;
cout << "Sum of two numbers value is : " << sum(a, b) << endl;
cout << "Difference of two numbers value is : "<< subtract(a, b) << endl;
cout << "Multiplication of two numbers value is : "<< multiply(a, b) << endl;
cout << "Division of two numbers value is : "<< divide(a,b) << endl;
return 0;
}
Q. Write an algorithm to addition, subtraction, multiplication and division in C++.
1. Start 2. Initialize two integer variables: - `a = 101` - `b = 2` 3. Define a function `sum(num1, num2)`: - Calculate the addition: `sum = num1 + num2` - Return `sum` 4. Define a function `subtract(num1, num2)`: - Calculate the difference: `sub = num1 - num2` - Return `sub` 5. Define a function `multiply(num1, num2)`: - Calculate the product: `mul = num1 * num2` - Return `mul` 6. Define a function `divide(num1, num2)`: - Calculate the division: `divi = num1 / num2` - Return `divi` 7. Call the functions with inputs `a` and `b`: - Call `sum(a, b)` and store result. - Call `subtract(a, b)` and store result. - Call `multiply(a, b)` and store result. - Call `divide(a, b)` and store result. 8. Print outputs: - Display "Sum of two numbers value is :" followed by the sum result - Display "Difference of two numbers value is :" followed by the difference result - Display "Multiplication of two numbers value is :" followed by the product result - Display "Division of two numbers value is :" followed by the division result 9. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com