##### C# Dash: Level 2 - Math on Track

# C# Dash: Level 2 - Math on track

### Sorry, saving conditionals for third tutorial.

So how did you do on the coding challenge on the last tutorial? Well I linked an example repl at the bottom of the second tutorial for some sample code for the last coding challenge.

Anyways, looks like another day of C# Dash. And we are about to start level 2, Math on Track.

## This tutorial will cover essential Math methods and variable type conversion.

## The user is expected to have read C# Dash: Level 1 - Object Oriented Madness (below), know what object oriented programming is, and know what square roots and other math around that level is.

DISCLAIMER:These tutorials will not cover complicated concepts, but will cover a basic selection of things to make some decent code.Some information was retrieved from the Microsoft .NET Online C# Documentation.

Words like **this** will appear in the glossary at the bottom.

# More Tutorials

# Scratch Tutorials

*SO LET'S GET STARTED!!!!!!!!!*

*SO LET'S GET STARTED!!!!!!!!!*

## Essential `Math`

Methods

So, if you want to have more complex math in your C# program, you will have to use a `Math`

**method**. I have a link to the .NET Online C# Documentation for math methods in my last tutorial.

I can't explain every math method in this tutorial, but here are a few essential ones:

`Math.Sqrt()`

- Returns the square root of inputted number.`Math.Cbrt()`

- Returns the cube root of inputted number.`Math.Max()`

- Returns the larger of two inputted numbers.`Math.Min()`

- Returns the smaller of two inputted numbers.`Math.Abs()`

- Returns the absolute value of inputted number.`Math.Pow()`

- Returns the number raised to whatever you input.- The first number is the one you want to raise.
- The second number is the power you are raising the first number by.

`Math.Floor()`

- Rounds a number down to the nearest integer.`Math.Ceiling()`

- Rounds a number up to the nearest integer.

Example of a `Math`

method:

```
using System;
class insert_class_name_here {
public static void Main (string[] args) {
double var1 = -3;
double var2 = Math.Abs(var1);
Console.WriteLine(var2);//Will output: 3
}
}
```

## Variable Conversion

Sometimes, you need to convert between types (like `string`

to `double`

) in order for the program to work. You write these methods for **variables**. This will show one method for that.

The method is `Convert`

. You can convert it to different things. These are the examples for the types I showed you.

`Convert.ToString()`

- Converts a non-string variable to a string.`Convert.ToChar`

- Converts a non-single-character variable to a single character (throws an**exception**if thing being converted has more than one character or is a double.)`Convert.ToInt32()`

- Converts a non-32-bit-integer variable to a 32-bit integer and removes numbers after the floating-point.`Convert.ToDouble()`

- Converts a non-double variable to a double.**NOTE:**Converting to an int or a double with letters will throw an exception.

Example of this kind of conversion:

```
using System;
class insert_class_name_here {
public static void Main (string[] args) {
string var1 = "5.2";
double var2 = Convert.ToDouble(var1);
Console.WriteLine(var2 - 4);//Will output: 1.2
}
}
```

That was one way to convert between types.

# Level Complete!

Next tutorial will be about conditionals and arrays.

# Coding challenge (optional)

Write a program that will take a number the user typed in and write the square root for that number. There will be variable conversion and `Math`

methods involved.

# Glossary

**Method**- Some code that is called with`()`

s. Examples include`Console.WriteLine()`

and`Math.Max()`

. Inside some methods, you put in arguments for output.**Variable**- Stored data under a name.**Exception**- An error occurring while program is running.