Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Multiplication, Division and Modulo | Operators
C Basics
course content

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

book
Multiplication, Division and Modulo

Multiplication Operator

The multiplication operator * yields the product of its operands. For instance, if you're trying to determine the amount of water required to fill a pool:

c

Main

copy
12345678910111213141516
#include <stdio.h> int main() { int height = 2; // in meters int width = 7; // in meters int length = 10; // in meters // A cubic meter contains one thousand liters int liters = (height * width * length) * 1000; printf("Size of pool: %d liters", liters); return 0; }

Division Operator

The division operator / divides the left operand by the right one.

Suppose you want to calculate a car's speed:

c

Main

copy
123456789101112
#include <stdio.h> int main() { int s = 200; // in meters double t = 3; // three hours double v = s / t; printf("Velocity = %f m/h", v); return 0; }

Using double variables ensures our result can be a decimal value, providing a more accurate answer. If we only worked with integer types, the result would also be an integer:

c

Main

copy
123456789101112
#include <stdio.h> int main() { int s = 200; // in meters int t = 3; // three hours // without `double v` variable printf("Velocity = %d m/h", s/t); return 0; }

It's important to note that merely changing the format specifier won't fix an incorrect integer division:

c

Main

copy
1234567891011
#include <stdio.h> int main() { int s = 200; // in meters int t = 3; // three hours printf("Velocity = %f m/h", s/t); // changed specifier return 0; }

However, there's a way to get a correct division without introducing another variable:

c

Main

copy
1234567891011
#include <stdio.h> int main() { int s = 200; // in meters double t = 3; // three hours printf("Velocity = %f m/h", s/t); // changed specificator return 0; }

Note

For a division to yield a decimal result, at least one of the operands should be of a decimal type like double.

Modulo Operator

The % operator returns the remainder of a division. For instance:

c

Main

copy
1234567891011
#include <stdio.h> int main() { printf("Modulo 8 %% 5 = %d\n", 8 % 5); printf("Modulo 10 %% 3 = %d\n", 10 % 3); printf("Modulo 7 %% 5 = %d\n", 7 % 5); printf("Modulo 20 %% 5 = %d\n", 20 % 5); return 0; }

Note

If you're trying to display the % character in a string (e.g., in a printf statement), you'd use %% to represent a single %. This tells the compiler you want to print the % character and not use it as a format specifier.

So, what's the value of the given expression?

So, what's the value of the given expression?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt