C# Lesson 1
Learn how to declare and use variables in the very first lesson from our series on learning C# for beginners.
Author
Learn how to do some basic math with C# in the 3rd lesson from our series for beginners learning C#.
In Lesson 3 we will learn how to do math with ints and doubles.
Afterwards you can continue to the next lesson!
Or go back and review the previous lesson.
We also have the source code on GitHub.
using System;
namespace Lesson3
{
class DoingMath
{
static void Main(string[] args)
{
//***********
//Console.Write("Enter an Int : ");
//int int1 = Convert.ToInt32(Console.ReadLine());
//Console.Write("Enter another Int: ");
//int int2 = Convert.ToInt32(Console.ReadLine());
////Console.WriteLine(int1 + " * " + int2 + " = " + int1 * int2); // 4, 2
////Console.WriteLine(int1 + " + " + int2 + " = " + int1 + int2);
////Console.WriteLine(int1 + " * " + int2 + " = " + (int1 * int2));
////Console.WriteLine(int1 + " + " + int2 + " = " + (int1 + int2));
//Console.WriteLine(int1 + " / " + int2 + " = " + (int1 / int2));
//Console.WriteLine(int2 + " / " + int1 + " = " + (int2 / int1));
//Console.WriteLine(int2 + " / " + int1 + " = " + (int2 / (double)int1)); // Downcasting or Explicit Cast
//Console.WriteLine(int2 + " / " + int1 + " = " + ((double)int2 / int1)); // Downcasting or Explicit Cast
//***********
//***********
Console.Write("Enter a Double: ");
double double1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter another Double: ");
double double2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(double1 + " / " + double2 + " = " + (double1 / double2)); // 4, 2
Console.WriteLine(double2 + " / " + double1 + " = " + (double2 / double1)); // .4, .2
Console.WriteLine(double1 + " + " + double2 + " = " + (double1 + double2));
Console.WriteLine(double1 + " - " + double2 + " = " + (double1 - double2));
// Computer isn't able to represent some numbers accuratly.
// The same way that .33 + .33 + .33 != 1
double addResult = double1 + double2;// .6000000000001
double roundedResult = Math.Round(addResult, 1);
Console.WriteLine(double1 + " + " + double2 + " = " + roundedResult);
}
}
}
/*
* TASK:
* Create a program that calculates an employees' pay.
* Ask for their Employee ID. This value will always be an INT.
* Ask for the number of hours they worked.
* Ask for their hourly pay rate.
*
* Example Output
* Enter employee ID: 1154856
* Enter hours worked: 16.55
* Enter hourly pay: 12.77
* Employee 1154856, you have earned $211.34
*
*/
using System;
namespace Lesson3Task
{
class DoingMathTask
{
static void Main(string[] args)
{
//NOTE: You will need to use convert.
//Step 1: Ask for the employee ID. Store the input from the user into an int variable.
//Step 2: Ask for the number of hours they worked.
//Step 3: Ask for their hourly pay.
//Step 4: Multiply hours by pay.
//Step 5: Round the result to 2 decimal places.
//Step 6: Display the result to the user.
}
}
}
We have similar articles. Keep reading!
Learn how to declare and use variables in the very first lesson from our series on learning C# for beginners.
Author
Learn how to read input from the user in the second lesson from our series on learning C# for beginners.
Author
You truly need this lesson from our beginner series if you aren't familiar with using a Boolean in C#.
Author