Clojure – Arithmetic Operators

clojure arithmetic operators

In this guide, we will discuss Clojure Arithmetic Operators. Clojure language supports the normal Arithmetic operators as any language. Following are the Arithmetic operators available in Clojure.

OperatorDescriptionExample
+Addition of two operands(+ 1 2) will give 3
Subtracts second operand from the first(- 2 1) will give 1
*Multiplication of both operands(* 2 2) will give 4
/Division of numerator by denominator(float (/ 3 2)) will give 1.5
incIncremental operators used to increment the value of an operand by 1inc 5 will give 6
decIncremental operators used to decrement the value of an operand by 1dec 5 will give 4
maxReturns the largest of its argumentsmax 1 2 3 will return 3
minReturns the smallest of its argumentsmin 1 2 3 will return 1
remRemainder of dividing the first number by the secondrem 3 2 will give 1

Example

The following code snippet shows how the various operators can be used.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (+ 2 2))
   (println x)
   
   (def x (- 2 1))
   (println x)
   
   (def x (* 2 2))
   (println x)
   
   (def x (float(/ 2 1)))
   (println x)
   
   (def x (inc 2))
   (println x)
   
   (def x (dec 2))
   (println x)
   
   (def x (max 1 2 3))
   (println x)
   
   (def x (min 1 2 3))
   (println x)
   
   (def x (rem 3 2))
   (println x))
(Example)

The above program produces the following output.

Output

4
1
4
2.0
3
1
3
1
1

Next Topic : Click Here

This Post Has One Comment

Leave a Reply