Calculate Values Of A Function - C# Programming Exercise

In this C# programming exercise, you need to create a program that calculates and displays certain values of the function y = x² - 2x + 1, using integer numbers for x ranging from -10 to +10. This exercise provides an excellent opportunity to practice loops and basic mathematical operations in C#.

The program will iterate from x = -10 to x = 10, computing the corresponding value of y for each x and displaying it on the screen. This type of exercise also reinforces the concept of applying mathematical formulas in programming.

Sample Output: For each value of x, the program will display the corresponding y value in a format like this:

 x = -10, y = 121 x = -9, y = 100 x = -8, y = 81 ... x = 10, y = 81 
This exercise is an excellent way to practice for loops in C# and understand how to compute mathematical functions in programming.

 Category

Basic Data Types

 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

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
        }
    }
}

 Share this C# exercise

 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#.

  •  Display a function

    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...

  •  Float, speed units

    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,...

  •  Sphere, float

    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...

  •  Vowel - switch

    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 ...

  •  Vowel - if

    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 ...

  •  Triangle, NorthEast

    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...