Calculate values of a function Learn programming Java

Lesson:

Basic Data Types


Exercise:

Calculate values of a function 70


Objetive:

Create a java program to display certain values of the function y = x2 - 2x + 1 (using integer numbers for x, ranging from -10 to +10)


Code:

public class Main
{
	public static void main(String[] args)
	{
		int y, x;

		System.out.println("y = x² - 2x +1");
		System.out.println();

		for (x = -10; x <= 10; x++)
		{
			y = x * x - 2 * x + 1;
			System.out.printf("x = %1$s ; y=(%1$s)² - 2*(%1$s) +1 = %2$s" + "\r\n", x, y);
		}
	}
}