Calculate values of a function Learn programming Java

Lesson:

Basic Data Types


Exercise:

Calculate values of a function


Objetive:

Create a program in java to display certain values of the function y = x^2 - 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);
		}
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.