w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: 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;
         
        Console.WriteLine("x = y² - 2y +1");
        Console.WriteLine();
         
        for (y=-5; y <= 5; y++)
        {
            x = y*y - 2*y + 1;
            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 

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?



New Content: Composer: Dependency manager for PHP, R Programming