The Assignment
An important element of C# code is the use of parentheses. Knowing when to use them, when not to use them, and how to apply them correctly is a vital skill for young coders to develop.
In the exercise below, your task is to correct the code so that it produces the desired output. Focus on placing parentheses where they are required and leaving them out where they are not.
Your mission is to fix the code before running the program and see if you can achieve the correct results.
The code
using System;
namespace Parenthesis
{
class Program
{
static void Main(string[] args)
{
{
int v = 2;
int result = v + 1 * 2;
Console.WriteLine("result: " + result + " expected result : 6");
result = v + 1 * v + 2 * 2;
Console.WriteLine("result: " + result + " expected result : 24");
result = v - 1 * 2 + 2 * 2;
Console.WriteLine("result: " + result + " expected result : 6");
result = v + v * v + v * 2;
Console.WriteLine("result: " + result + " expected result : 32");
result = v / 2 * v + 2 * 2;
Console.WriteLine("result: " + result + " expected result : 4");
}
}
}
}
The results that we are after
When you run the program you should see the following in the console window
result: 6 expected result : 6
result: 24 expected result : 24
result: 6 expected result : 6
result: 32 expected result : 32
result: 4 expected result : 4