Zebra0.com

csharp arithmeticC# Windows Forms Programming

C# Windows Forms Programming

Learn C# (C sharp) in FREE step-by-step lessons.

Arithmetic Operations

Arithmetic operators

C# has the following operators:

Operator Purpose Example x
+ addition x=5+3; 8
+ addition x=5+3.0; 8.0
- minus x=5-3; 2
- minus x=5-3.0; 2.0
* multiplication x=5*3; 15
* multiplication x=5*3.0; 15.0
/ division x=9/2; 4
/ division x=9/2.0; 4.5
% remainder x=14%3; 2

An actual program would not use a statement such as X=5+3; it would save time to simply use X=8. An actual program would be more likely to use variables: X=Y+Z; for example.

Note that there is a times, or multiplication, operator: *. In algebra, variables are always a single letter, XY in algebra means X times Y. In programming, variables can be several letters, and we could not be sure whether XY meant X times Y or a variable called XY.

Notice that operations on two integers results in an integer. If we divide 9/2 we get just 4. If we want 4.5 we must make one of the operands a double or float.

Remainder: %

The % operator is used to find the remainder. Before children learn about decimal numbers, they may give the answer to division problems as: "17 divided by 5 is 3 with a remainder of 2" Note that 17/5 results in 3, while 17 % 5 results in 2.

You can test any of these operations in C# with the form load event:

private void Form1_Load(object sender, EventArgs e)
{
this.Text = "23/5 =" + 23/5;
}

Please study the material at each of the links below.

  1. Microsoft Reference: C# Operators Microsoft Reference: C# Operators: C# Operators

  2. Drill: Operators Drill: Operators: Do this drill to make sure you understand

  3. Centering a control on the form Centering a control on the form: The control is centered when the form is resized
    /csharp/videos/csharp-centering.mp4
  4. Remainder operator: %

  5. Drill: The % operator Drill: The % operator: Drill on the % operator: remainder

  6. Finding Dozens: A program to select quantity on scroll bar and display dozens: quantity%12

  7. Finding Feet and Inches Finding Feet and Inches: A scroll bar selects inches, feet and inches is displayed
    /csharp/videos/csharp-inches2.mp4
  8. Order of operations: The Order of operations for arithmetic operations is to do parenthesis () then *, / %, then + and - working from left to right.

  9. Drill: Order of operations Drill: Order of operations: Test how well you can solve these problems

  10. Calculate Grade: Scroll bars select midterm and final exam, average is calculated

  11. Write a function: Write a function to avoid the same code in two places

  12. Algebra to code: Convert algebraic expressions to programming statements

  13. Decimal values on scroll bars: Value on scroll bar is integer, how to select a decimal value

  14. A function to display values on the scroll event and also form load A function to display values on the scroll event and also form load: Calculate tip, avoid misinformation at form load
    /csharp/videos/csharp-tip2.mp4
  15. Real world math: A few real world problems you can solve in C#

GlossaryGlossary for arithmetic lesson
Full Glossary