0

What is the aim of these lines in my code ?

Good day everyone, I just have a couple of questions that I wish you could help me at. So, here is the code. int a = 5, b = 4; int result = 1; (why did we assign result here ?) a=a/2; (why did we divide by 2 for a and b variables ?) b=b/2; if (a % 5 == 0 || b % 5 == 0) { result = 5; } else if (a % 4 == 0 || b % 4 == 0) { result = 4; } else if (a % 3 == 0 || b % 3 == 0) { result = 3; } else if (a % 2 == 0 || b % 2 == 0) { result = 2; } System.out.println("Result = " + result);

25th Oct 2022, 8:08 PM
Rakan Ezzedin
1 Respuesta
+ 6
Consider the following: int x; System.out.println(x); This results in compile error, because we never assigned a value to x, but we try to use it. This is the explanation for your first question. Creating a variable involves two steps: - declaration (of type), this is when memory is reserved for the variable - initialization, when a value is given These two steps can also happen together on the same line, but not necessarily. For your next question, the value of a and b are changed, effectively in your code both of them will be 2 (integer division 5/2 result is 2). Why change them? Well someone who wrote the code should be able to explain that :)
25th Oct 2022, 8:25 PM
Tibor Santa
Tibor Santa - avatar