Computer Applications
Give the output of the following program segment and also mention how many times the loop is executed.
int i;
for (i = 5 ; i > 10; i++)
System.out.println(i);
System.out.println(i * 4);
Java
Java Nested for Loops
2 Likes
Answer
20
Working
Initially i = 5
.
The test condition i > 10
is false and so the control comes to the next statement following 'for' loop — System.out.println(i * 4);
and prints 20
(5 * 4) on the screen.
Answered By
2 Likes
Related Questions
Find the error :
x = 3; y = 4; z = math.power(x*x, y/2);
Predict the output :
class FindFac { public static void main (String args[]) { for(int i = 2; i <= 100; i++) { System.out.print("Factors of" + i + ": "); for(int j = 2; j < i; j++) if((i % j) == 0) System.out.print(j + " "); System.out.println(); } } }
Predict the output :
class Power { public static void main(String args[]) { int e = 5, result, i; result = 1 ; i = e ; while(e > 0) { result *= 2 ; e--; } int n = result /2, p = i - 1; System.out.println("2 to the power of " + i + " is " + result); System.out.println("2 to the power of " + p + " is " + n); } }
Find the error
for(count = 0, count < 5, count = count + 1) System.out.println("This is count:" + count); System.out.println("Done!");