Display A Function - C# Programming Exercise

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 visually represented in the console using asterisks (*).

The program will calculate the value of y for each x in the specified range and display a line with as many asterisks as the value of y. This way, you generate a visual representation of the quadratic function. This exercise provides an opportunity to practice loops, mathematical calculations, and output formatting in C#.

This exercise allows students to visualize how mathematical calculations can be graphically represented using programming while practicing control structures like the for loop and string formatting.

 Category

Basic Data Types

 Exercise

Display A Function

 Objective

Write a C# program to "draw" the graphic of the function y = (x-4)2 for integer values of x ranging -1 to 8. It will show as many asterisks on screen as the value obtained for "y", like this:

*************************
****************
*********
****
*

*
****
*********
****************

 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 -1 to 8
        for (int x = -1; x <= 8; x++)  // Start from x = -1 and loop until x = 8
        {
            // Calculate the value of the function y = (x - 4)^2
            int y = (x - 4) * (x - 4);  // Function formula: y = (x - 4)^2

            // Print the asterisks corresponding to the value of y
            for (int i = 0; i < y; i++)  // Loop to print y number of asterisks
            {
                Console.Write("*");  // Print an asterisk for each iteration
            }

            // Move to the next line after printing the asterisks
            Console.WriteLine();  // Go to the next line after the current row of asterisks
        }
    }
}

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

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

  •  Prime factors

    In this C# exercise, you are asked to write a program that takes a number entered by the user and displays it as a product of its prime factors. Prime factors are numbers th...