Using Variables

Overview

In this lesson we focus on using variables in C# to find the product of two numbers and print it to the console using Visual Studio.

The Code

In this program you will write the following code:

class Program {

	static void Main(string[] args) {
		float factor1;
		float factor2;
		
		Console.WriteLine("Type your first number you want to multiply: ");
		factor1 = Convert.ToSingle( Console.ReadLine() );
		
		Console.WriteLine("Type your second number you want to multiply: ");
		factor2 = Convert.ToSingle( Console.ReadLine() );
		
		Console.WriteLine("The answer is: " + (factor1 * factor2) );
		
		Console.Read();
	}
	
}
  • Lines 8 & 11 – This line converts the user’s input from the string return type the Console.ReadLine() method returns to a float and stores it in our variable.

Checking for Understanding | Coding Errors

Some assignments lesson will have you repair broken code based on what you have learned. These lessons are shared via .NET Fiddle, which is an online IDE and complier for multiple coding languages. For these lessons you just need to find the errors, fix them, and copy and paste your code into the corresponding Google Form.

Deliverable

Take what you have learned this lesson and create some code that can solve two factor multiplication, subtraction or division equations (Bonus marks for attempting to get all four basic operations).

Assignments in this module are handed in via Google Classroom. Make sure you are signed into your PLSD account and click the link below:

Mark as Complete
chevron_right