-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Math Class important Fields and Methods
C# Math Class and Methods
- The System.Math class provides many constant fields and static methods that user can use to do logarithmic, trigonometric, and other mathematical calculations.
- Math.Max(a,b): It can be used to find the highest value of a and b variable (int, double, Decimal, Byte etc). Console.WriteLine(Math.Max(10, 20)); Console.WriteLine(Math.Max(10.677, 2.7889));
- Math.Min(x,y): This method is used to find the lowest value of of x and y variable. Console.WriteLine(Math.Min(10, 20));
- Math.Abs(x): It returns the absolute (positive) value of any number. double x = -3.78; Console.WriteLine(Math.Abs(x));
- Math.Sqrt(y): This method returns the square root of any specified variable. double y = 16; Console.WriteLine(Math.Sqrt(y));
C# Math Class and Methods - Contd
-
Math.Floor(x): Returns the largest integral value less than or equal to the specified number.
double x = -3.78; Console.WriteLine(Math.Floor(x)); //-4 Console.WriteLine(Math.Floor(5.38)); //5 - Math.Ceiling(x): Returns the smallest integral value greater than or equal to the specified number. double x = -3.78; Console.WriteLine(Math.Ceiling(x)); //-3 Console.WriteLine(Math.Ceiling(5.38)); //6
-
Math.PI Field: It represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI(π).
// To find PI constant values double pi_value = Math.PI; // Print result in console Console.WriteLine("Math.PI = " + pi_value); // input radius value of a cylinder double radius = 10; // input length value of a cylinder double length = 15; // calculate the area using PI of a cylinder double area = radius * radius * Math.PI; // Calculate volume of a cylinder double volume = area * length; // print area and volume of cylinder in console Console.WriteLine("Area of cylinder is : " + area); Console.WriteLine("Volume of cylinder is : " + volume);
C# Math Class and Methods - Contd
- Math.Pow(): This method is used to calculate a number raise to the power of some other number. // 5 is base and 3 is power, value will be 125 double pow_ab = Math.Pow(5, 3); // Print the result in Console Console.WriteLine(pow_ab); Console.WriteLine(Math.Pow(8, 2)); // 8 is base and 2 is power, value will be 64
- Math.Truncate(): This function takes in either a decimal or double data type, such as 20.55. It removes any fractional part of the number and returns the resulting integer value. Console.WriteLine(Math.Truncate(20.55)); // output: 20
- Math.Log(): This method returns the natural logarithm (base e) of a number. Console.WriteLine(Math.Log(3));
- Math.Round(x): Rounds a value to the nearest integer or to the specified number of fractional digits. Console.WriteLine(Math.Round(5.88)); //6 Console.WriteLine(Math.Round(5.38)); //5
Console.WriteLine(Math.Truncate(20.55)); // output: 20
Console.WriteLine(Math.Log(3));
// 5 is base and 3 is power
double pow_ab = Math.Pow(5, 3);// Print the result in Console
Console.WriteLine(pow_ab);
Console.WriteLine(Math.Pow(8, 2));double pi_value = Math.PI;
Console.WriteLine(pi_value);
// input radius value of a cylinder
double radius = 10;
// input length value of a cylinder
double length = 15;
// calculate the area using PI of a cylinder
double area = radius * radius * Math.PI;
// Calculate volume of a cylinder
double volume = area * length;// print area and volume of cylinder in console
Console.WriteLine("Area of cylinder is : " + area);
Console.WriteLine("Volume of cylinder is : " + volume);double x = -3.78;
double y = 16;
Console.WriteLine(Math.Round(5.88)); //6
Console.WriteLine(Math.Round(5.38)); //5Console.WriteLine(Math.Floor(x));
Console.WriteLine(Math.Floor(5.38)); //5
Console.WriteLine(Math.Ceiling(x)); //-4
Console.WriteLine(Math.Ceiling(5.38)); //5Console.WriteLine(Math.Max(10, 20));
Console.WriteLine(Math.Max(10.677, 2.7889));
Console.WriteLine(Math.Min(10, 20));
Console.WriteLine(Math.Min(10.677, 2.7889));
Console.WriteLine(Math.Abs(x));
Console.WriteLine(Math.Round(x));
Console.WriteLine(Math.Sqrt(y));