English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi!

I need to write a java program that asks the user to enter 2 numbers, x and y, and prints the remainder of x divided by y.

I know how to do this, its not the problem, my question is, how do you do it without using a java modulo operator?

I know how to write the script, im just not sure how to find a remainder of something without using mod - Only using division, multiplication and subtraction -


Thanks

2007-09-18 01:29:01 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

To do this without modulus, you need to divide the numbers, take the result and round it down to the next whole integer (with Math.round()). Multiply that integer by the divisor and then subtract that from the first number:

Say 10/3 = 3.3333. Your integer is 3 (rounding down 3.3333). Then 3*3=9. 10-9 is 1. So 10/3 = 3 R 1.

2007-09-18 01:39:38 · answer #1 · answered by Dr.Mr.Ed 5 · 0 0

input x and y.
z = x / y <=== ignore any remainder here
zz = z * y
remainder = z - zz

for example: use x = 5, and y = 2. In line 2, z should contain only an integer value of 2. The remainder has been lost to the bitbucket. In line 3, zz will contain the value of 4. Subtracting the value in zz (4) from the original value of x (5) yields the value of the remainder.

2007-09-18 01:41:29 · answer #2 · answered by Ralfcoder 7 · 0 0

what's the call of the JAVA record this code is contained in? If the record isn't at the instant named 'LotteryNumbers.java', then rename it with that call. no rely if that is already named that, or making that regulate does no longer fix the concern, then i'm uncertain what's incorrect.

2016-10-18 23:35:26 · answer #3 · answered by hussaini 4 · 0 0

fedest.com, questions and answers