Exercise
Calculate Values Of A Function
Objective
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)
Write Your C# Exercise
C# Exercise Example
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
}
}
}
More C# Programming Exercises of Basic Data Types
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this C# programming exercise, you must create a program to "draw" the graph of the function y = (x - 4)² for integer values of x ranging from -1 to 8. The result will be visuall...
In this C# exercise, you are asked to write a program that prompts the user for two important pieces of information: the distance in meters and the time taken,...
In this C# exercise, you are required to write a program that calculates the surface area and volume of a sphere, given its radius. The formulas provided are as follo...
In this C# exercise, you are asked to write a program that prompts the user for a symbol and, using the switch statement, determines whether the symbol entered is a ...
In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is a vowel (in lowercase), a digit, or any other ...
In this C# exercise, you are asked to write a program that prompts the user for a width and then displays a triangle where the base has the number of asterisks corresponding...