What would be the output of the following code segment if co

What would be the output of the following code segment if count variable is initialized with 5 for(int i = 1; i <= count; i += 2) { for(int j = 1; j <= i; j++){ System.out.print(\"*\"); } System.out.println(); }

Solution

output:

reason:

first time the inner for loop will execute once once as the condition j<=i is given where i value is 1 initially

now increment i by i+2,so becomes 3 from 1..so again inner loop due to condition j<=i will result in printing 3 stars

in last line again increment i by 2.so i becomes 5 and it executes 5 stars with help of inner loop.

now outer loop will fail as i value exceeds count value .so control does not go to ith loop

What would be the output of the following code segment if count variable is initialized with 5 for(int i = 1; i <= count; i += 2) { for(int j = 1; j <= i;

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site