⬅ Previous Topic
Type Conversion and Casting - Implicit vs ExplicitNext Topic ⮕
Comparison Operators⬅ Previous Topic
Type Conversion and Casting - Implicit vs ExplicitNext Topic ⮕
Comparison OperatorsArithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. They form the building blocks of any numerical computation in programming.
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulo (Remainder)Let’s take two numbers and apply all the arithmetic operators to understand their behavior.
START
SET a = 10
SET b = 3
PRINT "Addition: " + (a + b)
PRINT "Subtraction: " + (a - b)
PRINT "Multiplication: " + (a * b)
PRINT "Division: " + (a / b)
PRINT "Modulo: " + (a % b)
END
Output:
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulo: 1
Answer: In many programming environments, when both operands are whole numbers (integers), the division operation returns only the integer part (i.e., floor division). This is called integer division. If decimal accuracy is needed, you must explicitly convert operands to a floating-point type.
Let’s see how subtraction and modulo behave with negative numbers.
START
SET x = -15
SET y = 4
PRINT "Subtraction: " + (x - y)
PRINT "Modulo: " + (x % y)
END
Output:
Subtraction: -19 Modulo: -3
Answer: In some languages or environments, the modulo operation keeps the sign of the dividend (first number). Hence, -15 % 4 gives -3. Always check how your programming language implements modulo.
Can you swap two numbers without using a temporary variable?
START
SET a = 5
SET b = 7
a = a + b // a becomes 12
b = a - b // b becomes 5
a = a - b // a becomes 7
PRINT a, b
END
Output:
a = 7, b = 5
Answer: This works due to the reversible nature of addition and subtraction. By using these operators carefully, you can store and recover both original values without any extra space.
Let’s calculate the average of 3 scores:
START
SET math = 80
SET science = 90
SET english = 85
SET total = math + science + english
SET average = total / 3
PRINT "Total: " + total
PRINT "Average: " + average
END
Output:
Total: 255 Average: 85
⬅ Previous Topic
Type Conversion and Casting - Implicit vs ExplicitNext Topic ⮕
Comparison OperatorsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.