Going to come in handy when we’re writing more complex programs in this tutorial I want to talk to you guys about working with numbers and see now a lot of times when we’re writing programs in C we’re gonna want to do different things with numbers whether that’s just storing.
Numbers and keeping track of them or adding multiplying subtracting doing math with numbers or you know really just dealing with numbers.
In general so I’m just gonna give you guys a basic overview of a lot of the stuff we can do with numbers we’re also gonna talk about how different number types in Iraq so how like integers and doubles interact and all that fun stuff so down here I am printing out a number so you can see I’m using this.
Printf function and inside of these quotation marks I’m basically saying % F and % f.
Stands for like a floating-point number so that could be either a double or a float basically a decimal number so we put % F in here that’s gonna allow us to print out a decimal number just like this and you can see I can run my program and it’ll print out 8.9 and you’ll notice that it’s printing out not just 8.
It out to a very precise decimal point and that’s what C is always gonna do so it’s always gonna print it out to you know a very precise decimal so using these decimals I can.
Do all sorts of stuff so obviously I can just print out the decimal but I could also do things like math so I could say like five point zero plus four point five.
And this is gonna go ahead and be able to do that for us when I run.
My program not only is it gonna like print.
This out but it’s gonna do the actual math operation and you see we get nine point five so in addition to addition we can also use subtraction division which is going to be a forward slash and multiplication which is going to be in asterisks and you can use all these different things to let you know do the.
Four basic math operations addition.
Subtraction multiplication and division so now I want to talk about how we can use floating-point numbers so like doubles and floats alongside integers so let’s say I was going to add an integer with a double so if I said 5 plus 4.5 well actually when we do this this entire answer is going to get converted into a decimal number so when I say 5 which is an integer plus 4.5 we’re going to get a floating-point number back so we’re.5 and basically any operation that you do between an integer and a floating-point number is going to return a floating-point number back so that’s important to realize but if I was to change this to four now this isn’t gonna work anymore because this is asking for a floating-point number and we’re giving it an integer so when I do math with two integers it’s going to give.
Us an integer back and if I do math with a floating point number and integer it.
Will give us a floating-point number back it’s also important to realize for example if I took five and divided it by four this should actually be a decimal number right and you’d expect us to get a decimal back but if I just print it out percent D so I printed out the result of the integer five divided by the integer four I’m actually gonna get an integer back so when.
I run the program you’ll see we’re getting a one here but really this like the answer is one technically but it’s one with a bunch of decimal points after it right it’s.
Other stuff but when we just do math with two integers we’re gonna get an integer back if I was to say five divided by four point zero and I’ve printed out a decimal now we’re gonna get the entire answer back so we’re gonna get one point two five so that’s just a little bit about dealing with integers and floating.
Point numbers we could also take these and put these into variables so you know in addition to just having numbers down here I could specify a variable like you know num set it equal to six and we can go ahead and print num out down here as well I need to make this.
A D so now I will be able to print out that variable with no problem so it can be really useful sometimes to store these numbers inside of variables I also want to talk to you guys about using more complex mathematical functions now in C we have these things called functions which are basically just little blocks of code that we can call and.
When we call them they’re gonna go off and do certain things for us so they’re like modify a value or give us information about a value and these functions can be really useful I’m gonna show you guys a couple functions that we can use with math so I’m just going to come over here and I can actually start using these functions and all you have to do is just type out the name the function that you want to use so by default C is going.
To give us access to a bunch of math functions that we can use so we really don’t have to do anything all we have to do is just type in their names and I’m gonna show you guys want it’s just POWs so I’m just typing in P oh w then I’m gonna type an open and closed parenthesis and inside of these parentheses I can actually give this two numbers what this is gonna do is it’s gonna take the first number that we give it and it’s gonna take it to the second number so if I.
Said like 2 & 3 and you’ll notice I’m separating them with a comma this is basically gonna give us 2 raised to the third power so to give.
Us 2 cubed so when I run my program and actually we need to print this out as a decimal number so this is gonna give us a decimal number back so I need to print this out with F when I run my program we’re gonna get 8 back just like that so it’s basically.
Cubing 2 I could say like 4 raised to the third.
Power and now we should get back 64 which we do so that can be pretty useful there’s a couple other ones I’ll show.
You another one is SQ RT so this will allow you to.
Take a number square root so if I.
Said like 36 in here now we’re gonna print out the square root of 36 which is gonna be 6 and there’s a couple other ones so let’s.
Say that I had a decimal I could say C EIL which stands for ceiling so if I have like thirty six point seven or 36 point three five six this is gonna give us the hot the next highest number so it’ll basically round this number up and this is.