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