can someone simplify java code - for (int x=6; x<x*x;--x) { System.out.println(x): | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can someone simplify java code - for (int x=6; x<x*x;--x) { System.out.println(x):

can someone simplify it - for (int x=6; x<x*x;--x) { System.out.println(x):

7th Oct 2022, 8:10 AM
rohit
rohit - avatar
5 Answers
+ 6
x < x*x is true to all the integers except {-1,0,1}, if you start on 6 you will always stop on 1, so the code will print {6 5 4 3 2}. One way you can do that and it's to do: for (int x= 6; x > 1; x--) System.out.println(x); Note: Assintotically speaking both are the same, if you are trying to find all the integers below 7 that are less then their squares, your version is better since it's easier to understand. Now, if you want to print only 6 until 2 your version is hard to understand so to keep ir simple you use the one I proposed. Idk if I could answer your question, but good coding for you!
7th Oct 2022, 8:38 AM
Tomás Ribeiro
Tomás Ribeiro - avatar
+ 1
Where did you get the code, Challenges? x<x*x is tricky as Tomás explained you can also print that to see :)
7th Oct 2022, 8:52 AM
Daljeet Singh
Daljeet Singh - avatar
0
Yeah i got this code from challenge itself
7th Oct 2022, 9:21 AM
rohit
rohit - avatar
0
Tomás there should be x--
8th Oct 2022, 6:55 AM
zemiak
0
zemiak Yes, you're right, thanks. I already fixed it.
8th Oct 2022, 7:33 AM
Tomás Ribeiro
Tomás Ribeiro - avatar