This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/4/17

prime number divisible by 3

divinity divinity

2016/4/17

#
hi how do you write a java program that print prime number that is divisible by 3.
danpost danpost

2016/4/17

#
divinity wrote...
how do you write a java program that print prime number that is divisible by 3.
1
System.out.println("3");
That should answer your question because any other number divisible by 3 cannot be prime because it is divisible by 3.
divinity divinity

2016/4/20

#
hi how do you divide in java? the denominator must be divisible by the numerator. what i meant is like this 24/4. in java how do i do that. how do i do it by using the modulus.
danpost danpost

2016/4/20

#
If using 'int' type values, anything from 24 through 27 divided by 4 will result in a returned value of 6. The modulus operator, '%' will return the remainder of the operation; so, 24%4= 0, 25%4=1, 26%4=2 and 27%4=3.
divinity divinity

2016/4/20

#
okay now, how do you do it without using the modulus
danpost danpost

2016/4/20

#
divinity wrote...
okay now, how do you do it without using the modulus
I guess you would have to do it the long way:
1
2
3
4
5
6
7
int numA = 27;
int numB = 4;
 
int mod = numA;
while (numA >= numB) mod -= numB;
 
System.out.println("The remainder after dividing "+numA+" by "+numB+" is "+mod);
I did not put any checks to ensure that both values were positive.
divinity divinity

2016/4/20

#
hi danpost that is not working, i tried it and am getting an empty output. nothing is outputting. blank output
danpost danpost

2016/4/20

#
divinity wrote...
hi danpost that is not working, i tried it and am getting an empty output. nothing is outputting. blank output
Where did you try it? Show the code you used (method and all).
You need to login to post a reply.