Skip to main content
Logo image

Section 4.11 Division

Get ready to be surprised. But don’t worry, we’ll explain everything soon!

Subsection 4.11.1 Intro to Division in C

Activity 4.11.

    Try to predict what the output of the following program will be:

    admin.....open in new window

    Which of the following do you think will be printed to the screen?
  • 5 / 3 = 1
  • Correct! Do you have any idea why this might be happening?
  • 5 / 3 = 1.666667
  • Unfortunately, this is not what happens...
  • 5 / 3 = 1.67
  • Unfortunately, this is not what happens...
  • 5 / 3 = 2
  • Not quite - try again!
  • 5 / 3 = 1.7
  • Not quite - try again!

Subsection 4.11.2 Your Turn!

Activity 4.12.

Click on the Run button in the window below to reveal the integer division behavior that C exhibits. Next, change the data types of some of the variables (use float instead of int) and observe the outcome. What do you notice?

admin.....open in new window

Summary of division behavior in C:.

Following are the results of various examples of division in C. Notice the effect that the types of numbers being divided have on the quotient. Are there patterns? Why is this happening?
  • 5/2 = 2
    
  • 5.0/2 = 2.5
    
  • 5/2.0 = 2.5
    
  • 5.0/2.0 = 2.5