In: Computer Science
Write code that does the following: Opens a file named output.txt, uses a loop to write the even number from 2 through 100 to the file and then close the file. java based code please.
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
//file object to write into file
FileWriter wrt = new FileWriter("E://output.txt");
//variable declaration
int start = 2, end = 100;
for (int i = start; i <= end; i++) {
if (i % 2 == 0)
wrt.write(i + " ");
}
wrt.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}}}
Screenshot for reference:
====================