w3resource

C#: Program to display certain values of the function


C# Sharp Basic Data Types: Exercise-6 with Solution

Write a C# Sharp program to display certain values of the function x = y2 + 2y + 1 (using integer numbers for y, ranging from -5 to +5).

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise6
{
    public static void Main()
    {
        int x, y; // Declare integer variables x and y

        // Display the equation x = y² - 2y + 1
        Console.WriteLine("x = y² - 2y + 1");
        Console.WriteLine();

        // Loop to calculate and display x for different y values (-5 to 5)
        for (y = -5; y <= 5; y++)
        {
            // Calculate x using the equation x = y² - 2y + 1
            x = y * y - 2 * y + 1;

            // Display the value of y and the calculated value of x
            Console.WriteLine(
                "y = {0} ; x = ({0})² - 2*({0}) + 1 = {1}",
                y, x);
        }
    }
}

Sample Output:

x = y² - 2y +1                                                                                                 
y = -5 ; x=(-5)² - 2*(-5) +1 = 36                                                                             
y = -4 ; x=(-4)² - 2*(-4) +1 = 25                                                                             
y = -3 ; x=(-3)² - 2*(-3) +1 = 16                                                                             
y = -2 ; x=(-2)² - 2*(-2) +1 = 9                                                                              
y = -1 ; x=(-1)² - 2*(-1) +1 = 4                                                                              
y = 0 ; x=(0)² - 2*(0) +1 = 1                                                                                 
y = 1 ; x=(1)² - 2*(1) +1 = 0                                                                                 
y = 2 ; x=(2)² - 2*(2) +1 = 1                                                                                 
y = 3 ; x=(3)² - 2*(3) +1 = 4                                                                                 
y = 4 ; x=(4)² - 2*(4) +1 = 9                                                                                 
y = 5 ; x=(5)² - 2*(5) +1 = 16 

Visual Presentation:

C# Sharp Data Types: Program to display certain values of the function.

Flowchart:

Flowchart: Program to display certain values of the function.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program that takes the radius of a circle as input and calculate the perimeter and area of the circle.
Next: Write a C# Sharp program that takes distance and time as input and displays the speed in kilometers per hour and miles per hour.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.