(java)
a. The following code has compilation AND logic errors. Rewrite the code (using the while loop) so it will compile correctly and print all the even numbers from 0 to 10 on one line separated by a space:
int j = 0;
while j > 10 ;
j + 2;
System.println(j, “ “);
b.You want to print the values of integers a and b to an external file “test123.txt”, assigned to the PrintWriter variable output. Correct the following statements:
include java.io.*;
PrintWriter output = new (“test123”);
System.output.print(a, b);
close.output();
a.
The correct source code to print the all even number from 0 to 10 is given below:
//variable declaration and initialization
int j = 0;
//while loop
while(j<=10)
{
//print the value of j on the computer screen
System.out.print(j+" ");
j = j+2;
}
OUTPUT:
0 2 4 6 8 10
b.
The correct source code is given below:
import java.io.*;
//open the file
PrintWriter output = new PrintWriter("test123.txt");
//write into the file
output.print(a);
output.print(b);
//close the file
output.close();
Get Answers For Free
Most questions answered within 1 hours.