How to teach C to a violinist (Part IV)
Publicado por 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.
If you didn’t see Parts I, II and III you may like to read them.
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.
For instance:
float num=3.5;
float num2;
float num3=5.231;
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):
int a,b=3,c,d;
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.

rafinhaviolino escribió
See! I’m learning… at least now you dont need to say me tu put ; in the end of every line… x]~
But you need to tell me that %f is not the same of f%
(how can some1 be soooo stupid?!)
ok ok… the right one!
int
main () {
float a, b, c;
printf (“Enter a number: “);
scanf (“%f”,&a);
printf (“Enter another one: “);
scanf (“%f”,&b);
c=a/b;
printf (“%f/%f=%f”,a,b,c);
getchar();
getchar();
}
Yeahhh! I’m a lil genious!
jayzeegp escribió
I’m sure you are lil, but I’m not sure you are a genious
Well done!
rafinhaviolino escribió
Violin Hero em ação!!! x]~
Thank you for the exercises! I wont forget the printf thing anymore (I hope ^^’)
int
main () {
float a, b;
printf (“Enter a: “);
scanf (“%f”,&a);
printf (“Enter b: “);
scanf (“%f”, &b);
printf (“%f+%f=%f\n”,a,b,a+b);
printf (“%f-%f=%f\n”,a,b,a-b);
printf (“%f*%f=%f\n”,a,b,a*b);
printf (“%f/%f=%f”,a,b,a/b);
getchar ();
getchar ();
}
jayzeegp escribió
Well done! Look the main page