Wednesday, May 9, 2012

Expression Evaluation: Division

Hello guys!!! Hope, you all are well by the grace of Almighty. Today we learn, how to divide in C programming. Let's look at a program:













Output: Division: 10 / 3 = 3


This program is wrong because all of us know that 10 / 3 = 3.33. 3.33 is a float number. so, we can try this code by letting "s" as a float variable. let's look:













Output: Division: 10/3 = 3.000000


This program is wrong too because the result is wrong. so, we can try this code by letting all variables "a", "b" & "s" as a float variable. Let's look:













Output: Division: 10.000000 / 3.000000 = 3.333333


Now, this program is correct. But it is looking odd. If we want our output as 10 / 3 = 3.33, then we have to write this program in this way:


















Output: Division: 10 / 3 = 3.33


Now this program is perfect. In this program in line no. 8, i write "s=(float)a/b". This is called "Type Casting". We cast "a" to float from int variable. Why we cast ?? Because when divide two numbers, it's result and any of one must be in a float variable. If two numbers are integer then we have to cast any of these numbers. We can also write this code by following way:













Output: Division: 10 / 3 = 3.33

We can also Make an integer number to float number by adding ".0" with it.

Thank you all..:)

0 comments:

Post a Comment