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.

divinity02
6
Posting Whiz in Training

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.

Jump to Postfloat 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);

Jump to PostIf 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

rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster

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);

JamesCherrill
4,490
Most Valuable Poster
Moderator
Featured Poster

divinity02
6
Posting Whiz in Training

thnz j and rubberman

divinity02
6
Posting Whiz in Training

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);
```

JamesCherrill
4,490
Most Valuable Poster
Moderator
Featured Poster

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.

divinity02
6
Posting Whiz in Training

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.

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.