- 1

How the output is 18 can anyone understand me briefly please....

public class Program { public static void main(String[] args) { int res=0; for(int i=1; i<=10; i+=3){ if(10/i==2){ continue; } res+=i; } System.out.println(res); // output 18 } }

10th Oct 2020, 3:25 AM
born2code
born2code - avatar
3 Réponses
+ 2
Let's see what happens in every iteration :- i = 1 10/1 = 10 != 2 (condition false) res = 1 i = 4 10/4 = 2.5 == 2 (condition true) res = 1 i = 7 10/7 = 1.42 != 2(condition false) res = 1 + 7 = 8 i = 10 10/10 = 1 != 2 (condition false) res = 8 + 10 = 18 Thus answer is 18
10th Oct 2020, 3:34 AM
Arsenic
Arsenic - avatar
+ 2
Rankush please mark Arsenic s answer as best.
10th Oct 2020, 4:48 AM
Oma Falk
Oma Falk - avatar
0
Oh i understand i thinking wrong 10/4==2.5 or 2 ( condition false) Now understand clearly. Thank for answering me🙏
10th Oct 2020, 3:50 AM
born2code
born2code - avatar