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:
Main.c
123456789101112131415#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:
Main.c
1234567891011#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:
int + int => int
int + double => double
Main.c
1234567891011#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:
Main.c
12345678910#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:
Main.c
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; }
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:
Main.c
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; }
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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you give an example of using the multiplication operator?
How does integer division differ from division with doubles?
Can you explain how the modulo operator works with a real-life example?
Awesome!
Completion rate improved to 2.63
Multiplication, Division and Modulo
Swipe to show menu
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:
Main.c
123456789101112131415#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:
Main.c
1234567891011#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:
int + int => int
int + double => double
Main.c
1234567891011#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:
Main.c
12345678910#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:
Main.c
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; }
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:
Main.c
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; }
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.
Thanks for your feedback!