In this program, You will learn how to find quotient and remainder in R.
R Example: How to find quotient and remainder in R
x <- readline(prompt = "Enter first number :")
y <- readline(prompt = "Enter second number :")
x <- as.integer(x)
y <- as.integer(y)
q = as.integer(x / y)
q1 = x %/% y
q2 = x / y
r = x %% y
print(paste("Quotient with integer typecast :", q))
print(paste("Quotient with integer division operator:", q1))
print(paste("Quotient is :", q2))
print(paste("Remainder is :", r))
The code first reads in values for
y from the user and stores them as strings. It then converts
y to integers using the
Next, the code performs four different operations on
y: integer division, integer division using the
%/% operator, regular division, and modulo using the
%% operator. It stores the results of these operations in variables
Finally, the code prints the results of these operations to the console.
qis the result of integer division using typecasting. This means that the result of the division is converted to an integer by removing the fractional part. For example,
7/3would be converted to 2.
q1is the result of integer division using the
%/%operator. This operator performs integer division and returns the integer part of the division, discarding the remainder. For example,
7 %/% 3would return 2.
q2is the result of regular division using the
/operator. This operator performs division and returns the result as a floating point number. For example,
7 / 3would return 2.3333333.
ris the result of the modulo operation using the
%%operator. This operator returns the remainder of the division. For example,
7 %% 3would return 1.
Enter first number :5
Enter second number :3
 "Quotient with integer typecast : 1"
 "Quotient with integer division operator: 1"
 "Quotient is : 1.66666666666667"
 "Remainder is : 2"