hi everyone

I have a question: how do you divide in java. by it being that the first number must be divisible by the second number.

Answered by rubberman 1,355 in a post from

float x = 10.0;
float y = 2.0;
float z = x/y;

System.out.printf("x == %f, y == %f, x/y == %f\n", x, y , z);

Answered by JamesCherrill 4,490 in a post from

If x is exactly divisible by y then that means x modulo y must be zero (ie the remainder is zero). Java uses the % operator for modulo

## All 6 Replies

float x = 10.0;
float y = 2.0;
float z = x/y;

System.out.printf("x == %f, y == %f, x/y == %f\n", x, y , z);

If x is exactly divisible by y then that means x modulo y must be zero (ie the remainder is zero). Java uses the % operator for modulo

thnz j and rubberman

hello james, rubberman, stultuske

I am trying to get the last round of this game to generate random numbers from 1 to 25 and the second random numbers to generate from 2 to 9 and of which the first number must be divisible by the second numbers.
i am wondering if the codes that i have created will be able to do just as the question ask.

note that: I haven't finished coding as yet

here is the codes that I have created so far.

``````for(i = 9; i<=10; i++)
{
rand  = 1 + randobj.nextInt(25);
rand2 = 2 + randobj.nextInt(8);
correctans = rand / rand2;
incorrectans = 25 + randobj.nextInt(59);

while(num1 %2 != 0) would this work
{
if(num1 % num2 !=0)this also, will it work or do i have it wrong
{
}
}
System.out.println(playername+"round 3"+ "level "+ i+ "question "+i+ "you are required to match the letter with the correct answer");
System.out.println(playername+" please divide "+ rand+ " / "+ rand2);

System.out.println("A:"+correctans);
System.out.println("B:"+ incorrectans);
choice = userinput.next().charAt(0);
``````

generate random numbers from 1 to 25 and the second random numbers to generate from 2 to 9 and of which the first number must be divisible by the second numbers.

One approach is trial and error - easy to code but inefficient in execution (not a problem with these values, but does not scale well)

``````do
generate both random numbers
while (their modulo is not zero)
``````

another way is to generate the second random number (2-9) and a random multiplyer (1- 25/firstnumber) . Mutliply those together to get the first number. This will always work first time.

i got the division part also to work thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.