Problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem

Hey guys! Can you help me and tell me what ia wrong with tis code? public class Program { public static void main(String[] args) { int x=6; int y=15; int z; if(x<y) { z=x; } else{ z=y; } if(z%x<=0 && z%y<=0){ System.out.println("z"); z-=z; } } } I need the fix, and I cant find it, it sais "No output"

25th May 2017, 5:27 PM
MysticMM
MysticMM - avatar
15 Answers
+ 1
Hi, your result is No output because you don't print anything Explaination: x = 6 and y = 15 => x<y then z = 6 you want test that : if(z%x<=0 && z%y<=0) so z%x => 6%6 = 0 then z%x <= 0 return true and z%y => 6%15 = 6 then z%y <= 0 return false true && false return false your test is always false then you won't execute System.out.println("z"); What do you really want to test ?
25th May 2017, 5:39 PM
MBZH31
MBZH31 - avatar
+ 6
The code is fine and does what it should do. It says no output because the if condition results in false. if(z%x<=0 && z%y<=0) The first condition is true because z is 6 and x is 6. But the second one is false because z is 6 and y is 15. So first one results in 0 but the second one results in 6
25th May 2017, 5:36 PM
Tim G
Tim G - avatar
+ 1
Oh right and I put the z-=z in the wrong spot.
25th May 2017, 5:37 PM
MysticMM
MysticMM - avatar
0
Vhy isnt it working if i put z-=z in the next layer?
25th May 2017, 5:39 PM
MysticMM
MysticMM - avatar
0
I wanna test if x and y can be divided by same number (being z) So i put z-=z one layer down and it still isnt working.
25th May 2017, 5:41 PM
MysticMM
MysticMM - avatar
0
if you want test if x and y can be divided by same number (being z), change your modulo expression if (x%z == 0) && (y%z == 0){ System.out.println("x and y are divising by z"); } else { System.out.println("both x and y are not divising by z"); } i don't understand why you want do z -= z this return z = 0
25th May 2017, 5:50 PM
MBZH31
MBZH31 - avatar
0
z-=z is there because i wanna get the number that can be divided by both, if z=6 is not dividable, it will try 5,4,3.... until it gets the number that can be dividable by both, in this case 3.
25th May 2017, 6:18 PM
MysticMM
MysticMM - avatar
0
if (x%z == 0) && (y%z == 0){ msg = x + " and " + y + " are divising by " + z; System.out.println(msg); } else { System.out.println("both " + x + " and " + y + " are not divising by " + z); z -= 1; // or z--; }
25th May 2017, 6:27 PM
MBZH31
MBZH31 - avatar
0
i forgot but you must include this code within a while loop with condition z>0
25th May 2017, 6:37 PM
MBZH31
MBZH31 - avatar
0
Oh okay, Ill try
25th May 2017, 6:46 PM
MysticMM
MysticMM - avatar
0
It worked! Thanks!
25th May 2017, 6:51 PM
MysticMM
MysticMM - avatar
0
What
25th May 2017, 6:54 PM
MysticMM
MysticMM - avatar
0
I published it, can you tell me the name for it, I am not the best in English .. And I made so that it tell all dividable numbers.
25th May 2017, 6:55 PM
MysticMM
MysticMM - avatar
- 2
like like ... a lot of like please
25th May 2017, 6:54 PM
MBZH31
MBZH31 - avatar
- 2
PPCM in french LCM in english like = upvote
25th May 2017, 7:07 PM
MBZH31
MBZH31 - avatar