122k views
1 vote
Write a Java program that writes the x and y of the formula
y=x^2+2x+1 to a csv file

1 Answer

1 vote

Final answer:

To write a Java program that writes the x and y values of the equation y = x^2 + 2x + 1 to a CSV file, you can use the FileWriter class along with a BufferedWriter.

Step-by-step explanation:

To write a Java program that writes the x and y values of the equation y = x^2 + 2x + 1 to a CSV file, you can use the FileWriter class along with a BufferedWriter. Here's an example:

import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

public class Main {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.csv");
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write("x,y\r\\");

for (int x = 0; x <= 10; x++) {
int y = x * x + 2 * x + 1;
bufferedWriter.write(x + "," + y + "\r\\");
}

bufferedWriter.close();
System.out.println("CSV file created successfully.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

User Georg Pfolz
by
8.1k points