How to teach C to a violinist (Part IV)
Posted by jayzeegp en julio 16, 2008
Tired of just using integer numbers?
On this entry we will learn how to use floating point numbers (such as 3.45321) in an easy way.
To declare a floating point number you must do the same that you did with integers but just using the type float instead of int.
But there are things about declarations that I didn’t tell you on Part I (because I wanted it to be simple).
Can I declare several variables on the same line?
I’m happy that you make this question because I was just going to explain it to you.
The answer is yes, but just of the same type.
I will show you an example (as always):
float ferkiwi=6.9, skate, fable=7.3, junior=0.01;
This will create 4 int variables (a,b,c and d) and just one would have a value on declaration (b which values 3) and also 4 float variables (ferkiwi, skate, fable and junior), having values 3 of them.
Let my show you my skills!
Ok! Two exercises more:
1.- Make a program which asks for 2 floating point numbers and then shows the result of the division of them.
Note: on scanf and printf when you use floating point numbers, you must put %f instead of %d.
2.- Make a program which asks for 2 floating point numbers and then shows the result of the addition, multiplication, substraction and division of them USING JUST 2 VARIABLES.
Note: to use just 2 variables you can read the comments on Part III.