Computer Applications
Give the output of the following program segment:
for(k=a;k<=a*b;k+=a)
{ if (k%b==0)
break;
}
System.out.println(k);
Give the output when a = 6, b = 4.
Java Iterative Stmts
1 Like
Answer
Output
12
Explanation
1. The loop starts with k = 6.
2. The loop runs while k <= 6 * 4, i.e., k <= 24.
3. In each iteration, k increases by 6.
4. The loop checks if k % 4 == 0. If true, it breaks the loop.
Iterations:
- First:
k = 6,6 % 4 = 2→ Not 0 → Loop continues - Second:
k = 12,12 % 4 = 0→ Condition true → Loop breaks
5. The current value of k is 12. So, the output is: 12.
Answered By
1 Like
Related Questions
A manager wants to check the number of employees with names ending with KUMAR, and fill in the blanks in the given program segment with appropriate Java statements:
void count(String s[]) { int i, l=_________, c=0; for(i=0;i<l;i++) { if(________________) c++; } System.out.println(c); }The output of a program which extracts a part of the string "COMPASSION" is as follows:
(a) "PASS"
(b) "PASSION"Write appropriate Java statements to get the above outputs.
Kamal wants to display only the alphabets stored in a character array. While compiling the code, an error was displayed. Check for the statement with the error and write the correct statement.
char arr[]={'4', '&', 'a', 'w', 'd'}; int i; for(i=0;i<arr.length;i++) { if(Character.isLetter(arr)) System.out.println(arr); }Name the type of type casting in the given statements:
(a) double m = 'b';
(b) char ch = (char)68;