7/7/2023 0 Comments Simple math equation typesThere's a little more going on here than that, but we'll discuss it more in a few tutorials. If you run this code, you will see that the result is the computer prints out, "The area of the circle is 50.26548". It knows that you can't "add" strings in a mathematical sense, but there is an intelligent way of handling it: to concatenate the text together so that one follows the other. Notice, too, that we've been able to use the '+' operator with strings (text). We then print out the result for the user. In the above code snippet, we have variables for the radius of a circle, the value of the number PI, and the area, which we calculate using a standard formula from geometry class. WriteLine ( " The area of the circle is " + area ) Note how using '+' with a string (text) "concatenates" the number on the end of the text. The formula for the area of a circle is pi * r ^ 2 float radius = 4 įloat pi = 3.1415926536 f // The 'f' at the end makes it treat it as a float instead of a double. The following code adds the numbers '3' and '4' and stores the result in the variable called a: Let's start with a simple math problem that you probably did in second grade. Addition, Subtraction, Multiplication, and Divisionĭoing the basic operations of addition, subtraction, multiplication, and division should be pretty straightforward, especially if you have done other programming before. In this tutorial, we'll cover how to do some basic arithmetic in C#, starting with addition and subtraction, and move up through multiplication and division, a new operation called the modulus operator, positive and negative numbers, order of operations, and conclude with some compound assignment operators, which do math and assign a value all at once. In fact, it is basically all they can do. This math is all pretty easy-the kind of stuff you learned in elementary school. With a basic understanding of variables behind us, we're ready to get into some deeper material: math. More issues with math in C# will be discussed in a couple of tutorials. So for instance, a += 3 is the same as a = a + 3, and essentially adds 3 to the value of a. Compound assignment operators ( +=, -=, /=, *=, and %=) do the desired operation and assign it to the variable on the left.Parentheses come even before multiplication and division, so you can use parentheses to make something happen before something else: double m = (y2 - y1) / (x2 - x1) Order of operations matters, and it works just like you learned in math class, with multiplication and division happening before addition and subtraction, in a left to right order.You can combine multiple operations into one, as well as use a previously created variable in a statement: int f = 3 * b - 4.You can use any of the standard numerical types ( double, float, int, short, etc.) for doing math.The modulus operator ('%') gets the remainder of a division operation like this: int e = 22 % 7 (In this case, the variable e will be 1.).Division uses the forward slash character ('/') like this: int d = 21 / 7.Multiplication uses the asterisk character ('*') like this: int c = 4 * 3.Subtraction works like this: int b = 7 - 2.Addition works like this: int a = 3 + 4.Arithmetic in C# is very similar to virtually every other programming language out there, particularly C++, C, and Java.
0 Comments
Leave a Reply. |