Exercise
Calculate values of a function
Objetive
Write a C# program in C# to display certain values of the function y = x^2 - 2x + 1 (using integer numbers for x, ranging from -10 to +10)
Example Code
using System; // Import the System namespace to use basic classes like Console
class Program // Define the main class of the program
{
static void Main() // The entry point of the program
{
// Iterate over integer values of x from -10 to 10
for (int x = -10; x <= 10; x++) // Start from x = -10 and loop until x = 10
{
// Calculate the value of the function y = x^2 - 2x + 1
int y = (x * x) - (2 * x) + 1; // Function formula: y = x^2 - 2x + 1
// Display the value of x and the corresponding y
Console.WriteLine($"For x = {x}, y = {y}"); // Output the result for the current value of x
}
}
}