Arithmetic Operators in Solidity
Arithmetic operators are used to perform arithmetic or mathematical operations. Solidity has the following types of arithmetic operators:
- Addition: The addition operator takes two operands and results in a sum of these operands. It is denoted by +.
- Subtraction: The subtraction operator takes two operands and results in a difference between these operands. It is denoted by -.
- Multiplication: The multiplication operator takes two operands and results in a product of these operands. It is denoted by *.
- Division: The division operator takes two operands and results in a quotient after the division of these operands. It is denoted by /.
- Modulus: The modulus operator takes two operands and results in the remainder after the division of these operands. It is denoted by %.
- Increment: The increment operator takes one operand and increments the operand by one. It is denoted by ++.
- Decrement: The decrement operator takes one operand and decrements the operand by one. It is denoted –.
Operator | Denotation | Description |
---|---|---|
Addition |
+ |
It results in the sum of two operands. |
Subtraction |
– |
It results in the difference between the two operands. |
Multiplication |
* |
It results in the product of two operands. |
Division |
/ |
It results in the quotient of the division of two operands. |
Modulus |
% |
It results in the remainder of the division of two operands. |
Increment |
++ |
It increments the operand by one. |
Decrement |
— |
It decrements the operand by one. |
Below is the Solidity program to implement Arithmetic operators:
Solidity
// Solidity program to implement // Arithmetic Operators pragma solidity ^0.5.0; // Creating Contract contract Arithmetic { function arithop(uint a, uint b) public pure returns (uint, uint, uint, uint, uint, uint, uint) { // Addition operator uint sum = a + b; // Subtraction Operator uint sub = a - b; // Multiplication Operator uint mul = a * b; // Division Operator uint div = a / b; // Modulus Operator uint mod = a % b; // Increment Operator uint inc = ++a; // Decrement Operator uint dec = --b; return (sum, sub, mul, div , mod, inc, dec); } } |
Output:
Contact Us