In this program, You will learn how to find quotient and remainder in R.
R Example: How to find quotient and remainder in R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ 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 x
and y
from the user and stores them as strings. It then converts x
and y
to integers using the as.integer
function.
Next, the code performs four different operations on x
and y
: integer division, integer division using the %/%
operator, regular division, and modulo using the %%
operator. It stores the results of these operations in variables q
, q1
, q2
, and r
, respectively.
Finally, the code prints the results of these operations to the console.
q
is 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/3
would be converted to 2.q1
is 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 %/% 3
would return 2.q2
is the result of regular division using the/
operator. This operator performs division and returns the result as a floating point number. For example,7 / 3
would return 2.3333333.r
is the result of the modulo operation using the%%
operator. This operator returns the remainder of the division. For example,7 %% 3
would return 1.
1 2 3 4 5 6 7 8 |
Enter first number :5 Enter second number :3 [1] "Quotient with integer typecast : 1" [1] "Quotient with integer division operator: 1" [1] "Quotient is : 1.66666666666667" [1] "Remainder is : 2" |