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 3[9 marks]Code Snippet:This program prints all even numbers from 1 to 20 separated by commas usinga StringBuilder within a while loop.public class EvenNumbersPrinter {public static void main(String[] args){int number =1;StringBuilder evenNumbers = new StringBuilder();while (number <=20){if (number %2==0){evenNumbers.append(number);evenNumbers.append(",");}number++;}System.out.println("Even numbers from 1 to 20: "+ evenNumbers);}}4 HSYD100-1-July-Dec2024-FA2-RR-V.2-140520243.1 What is the purpose of the StringBuilder object evenNumbers in this codesnippet? (2 marks)3.2 How does the while loop condition ensure that only even numbers areappended to the evenNumbers StringBuilder object? (2 marks)3.3 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 1 to 20separated by commas. Identify and explain the advantages of using a for loopcompared to a while loop. (3 marks)3.4 Define and explain the advantages of using a for loop compared to a whileloop. (2 marks)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!