Adds two numbers |
Subtracts two numbers |
Multiplies two numbers |
Divides two numbers |
For the multiplication process, make two versions: - one that uses the multiplication operator - and another that uses a loop. |
For the division process, implement a check so the program does not divide by zero. |
pragma solidity ^0.8.0;
contract MathLibrary1 {
// This variable is available to easily view the result of the math operation
int public result;
function sum(int num1, int num2) public {
int sum = num1 + num2;
result = sum;
// This function can also be shortened to just
// result = num1 + num2;
}
function difference(int num1, int num2) public {
result = num1 - num2;
}
function multiplyOperator(int num1, int num2) public {
result = num1 * num2;
}
function multiplyLoop(int num1, int num2) public {
int product = 0;
for (int i = 0; i < num2; i++) {
product += num1;
}
result = product;
}
function divide(int num1, int num2) public {
// Note that this function will not return rational numbers
if (num2 == 0) {
// If a division by zero case were to occur, the result will be set to a default 0
result = 0;
}
else {
result = num1 / num2;
}
}
}