Question: Question 3 [ 9 marks ] Code Snippet:This program prints all even numbers from 1 to 2 0 separated by commas usinga StringBuilder within a
Question marksCode Snippet:This program prints all even numbers from to separated by commas usinga StringBuilder within a while loop.public class EvenNumbersPrinter public static void mainString argsint number ;StringBuilder evenNumbers new StringBuilder;while number if number evenNumbersappendnumber;evenNumbers.append;number;Systemout.printlnEven numbers from to : evenNumbers; HSYDJulyDecFARRV What is the purpose of the StringBuilder object evenNumbers in this codesnippet? marks How does the while loop condition ensure that only even numbers areappended to the evenNumbers StringBuilder object? marks Rewrite the provided code snippet to use a for loop instead of a while loop toachieve the same functionality of printing all even numbers from to separated by commas. Identify and explain the advantages of using a for loopcompared to a while loop. marks Define and explain the advantages of using a for loop compared to a whileloop. marks
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
