Open In App

Arithmetic Operators in Solidity

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Arithmetic operators are used to perform arithmetic or mathematical operations. Solidity has the following types of arithmetic operators:

  1. Addition: The addition operator takes two operands and results in a sum of these operands. It is denoted by +.
  2. Subtraction: The subtraction operator takes two operands and results in a difference between these operands. It is denoted by -.
  3. Multiplication: The multiplication operator takes two operands and results in a product of these operands. It is denoted by *.
  4. Division: The division operator takes two operands and results in a quotient after the division of these operands. It is denoted by /.
  5. Modulus: The modulus operator takes two operands and results in the remainder after the division of these operands. It is denoted by %.
  6. Increment: The increment operator takes one operand and increments the operand by one. It is denoted by ++.
  7. 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:

Arithmetic Operators

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads