Computer Applications
How many times will the following loop execute? Write the output of the code:
for(int j = 12; j >= 2; j -= 2) {
if(j % 5 == 0)
continue;
System.out.println(j);
}
Java Iterative Stmts
Answer
Loop executes six times.
Output
12
8
6
4
2
Reason
Step 1: Understanding the Loop
- Initialization:
j = 12
- Condition:
j >= 2
(Loop runs whilej
is≥ 2
) - Update:
j -= 2
(Decrementj
by 2 each iteration) if (j % 5 == 0) continue;
- Skips printing if
j
is divisible by5
.
Step 2: Iterations Breakdown
Iteration | Value of j | Condition j % 5 == 0 | Action |
---|---|---|---|
1st | 12 | Not divisible by 5 | Print 12 |
2nd | 10 | Divisible by 5 | Skip (continue) |
3rd | 8 | Not divisible by 5 | Print 8 |
4th | 6 | Not divisible by 5 | Print 6 |
5th | 4 | Not divisible by 5 | Print 4 |
6th | 2 | Not divisible by 5 | Print 2 |
Step 3: Final Output
12
8
6
4
2
Answered By
Related Questions
Define a class to accept a four digit number and check whether it is a Framul Number or not. A number is called a Framul Number if the product of all its digits equals fives times the sum of its first and last digit.
Example 1:
Input: 1325
Product of all digits = 1 x 3 x 2 x 5 = 30
Five times sum of first & last digit = 5 x (5 + 1) = 5 x 6 = 30Output: Framul Number
Example 2:
Input: 2981
Product of all digits = 2 x 9 x 8 x 1 = 144
Five times sum of first & last digit = 5 x (1 + 2) = 5 x 3 = 15Output: Not a Framul Number
If the entered number is not of four digits, the message "Please enter a four digit number" should be shown.
import java.util.Scanner; public class KboatFramulNumber { public static void main(String args[]) { Scanner sc = _____(1)_____ System.out.print("Enter a four-digit number: "); int num = _____(2)_____ _______(3)_______ { System.out.println("Please enter a four-digit number"); return; } int d = 0; int f = num % 10; int p = 1; _______(4)_______ { _____(5)_____ _____(6)_____ _____(7)_____ } int s = _____(8)_____ if (p == s) { _______(9)_______ } else { _______(10)_______ } } }
Define a class to accept a number from user and check if it is an EvenPal number or not.
(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even numberRewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );
How many times will the following loop execute?
int a = 5; while (a > 0) { System.out.println(a-- + 2); if (a % 2 == 0) break; }