Updated April 20, 2023
Introduction of Lua Modulo
The Lua modulo operator is used to get the remainder of a division. The modulo operator is a built-in operator in the lua programming. Modulooperatoris an athematic operator and it is denoted by the % symbol. The modulo operation return the whole number remainder, instead of the division result.
Syntax:
a % b
Parameters:
- a: This is not optional, that specifies the operand an (as a dividend), which is of an integer data type.
- b: This is not optional, that specifies the operand b (as a divisor), which is of an integer data type.
Return Value: The return value of the lua modulo operator is 0, if the a is completely divisible by b. The return value can be in the range [ 1, a-1 ], if a is not completely divisible by b. The modulo operator can also be throw error in the case if a is 0 (because it is division by zero error).
Working of Modulo Operator in Lua Programming
The working of the modulo operator in lua programming:
The modulo operator is used to get the remainder of the division. The modulo operator operates on the two operand, the left side operand as the dividend and the right side operand as the divisor. Consider an example, “res = 6 % 2” Here dividend is 6, and the divisor is 2 after 6 is divided by 2, the quotient will be 3, and the remainder will be 0, so the output of this expression would be 0 as the remainder its return.
Examples
Example of modulo operator in lua programming to show the usage of modulo operator-
Example #1
Code:
-- To store two integer values
a = 30;
b = 40;
print("The variable a is : ", a);
print("The variable b is : ", b);
--To store the result of the modulo expression
result = a % b;
print("The result of 30 modulo 40 is : ", result);
result = b % a;
print("The result of 40 modulo 30 is : ", result);
a = 40;
b = 20;
result = a % b;
print("The variable a is : ", a);
print("The variable b is : ", b);
print("The result of 40 modulo 20 is : ", result);
Output:
As in the above lua program, the a and b variables are created and store some different values to check the modulo operator behavior. In the first case “The result of 30 modulo 40 is : 30″( because 30 divide by 40 remainder is 30), in the second case “The result of 40 modulo 30 is : 10” and in the third case “The result of 40 modulo 20 is : 0”.
Example #2
Example of modulo operator in lua programming to show the behavior on the negative and float data types –
Code:
-- To store two integer values
a = -30;
b = 40;
print("The variable a is : ", a);
print("The variable b is : ", b);
--To store the result of the modulo expression
result = a % b;
print("The result of -30 modulo 40 is : ", result);
a = 30;
b = -40;
result = a % b;
print("The result of 30 modulo -40 is : ", result);
a = -30;
b = -40;
result = a % b;
print("The variable a is : ", a);
print("The variable b is : ", b);
print("The result of -30 modulo -40 is : ", result);
a = 40.5;
b = 30.5;
result = a % b;
print("The variable a is : ", a);
print("The variable b is : ", b);
print("The result of 40.5 modulo 30.5 is : ", result );
Output:
As in the above lua program, the a and b variables are created and store some different negative and float values to check the modulo operator behavior. In the first case “The result of -30 modulo 40 is : 10”, in the second case “The result of 30 modulo -40 is : -10”, in the third case “The result of -30 modulo -40 is : -30” and in the fourth case “The result of 40.5 modulo -30.5 is : 10”.
Example #3
Example of modulo operator in lua programming to show the usage of modulo operator to find whether the number is even or odd –
Code:
-- create a variable and initialize by some value
num = 20
print("The number is : ", num)
-- checking whether the number is even or odd
if (num % 2 == 0) then
print("The Number is even.")
else
print("The Number is odd.")
end
Output:
As in the above lua program the num variable is created to store the number as 20, next in the code the num variable is checking whether it have an even or odd number by using the if statement. If the num have an even number then print “The Number is even.”, else print “The Number is odd” as we can see in the output also.
Recommended Articles
We hope that this EDUCBA information on “Lua Modulo” was beneficial to you. You can view EDUCBA’s recommended articles for more information.