GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Multiplicative operators in C

There are three multiplicative operators.

  1. Multiplication (*)
  2. Division (/)
  3. Modulo (%)

We shall discuss these one by one.

You may also refer to the video posted at the end of this page.

Multiplication (*)

This operator multiplies two numbers.

Ex: 

2*3 = 6, 

2*3.1 = 6.2

 

Division (/)

This operator divides the two numbers. There are some rules for division.

  1. integer / integer = integer
  2. integer / real_number = real_number
  3. real_number / integer = real_number
  4. real_number / real_number = real_number

It means if either of numerator or denominator is real_number the final answer is real_number.

Let us understand this with some examples:

2/3 = 0.67 (mathematically), but we see that 2 is an integer (no decimal point) and 3 is an integer. And we know that integer/integer = integer. Therefore,

2/3 = 0 (truncate all the digits after decimal point)


 2.0/3 = 0.67 (mathematically), but we see that 2.0 is a real number (decimal point) and 3 is an integer. And we know that real_number/integer = real_number. Therefore,

 2.0/3 = 0.67 (since 0.67 is a real number)


 2/3.0 = 0.67 (mathematically), but we see that 2 is an integer (no decimal point) and 3.0 is a real number. And we know that integer/real_number = real_number. Therefore,

 2/3.0 = 0.67 (since 0.67 is a real number)


 2.0/3.0 = 0.67 (mathematically), but we see that 2.0 is a real number (decimal point) and 3.0 is a real number. And we know that real_number/real_number = real_number. Therefore,

 2.0/3.0 = 0.67 (since 0.67 is a real number)


Similarly,

  1. 4.0/2 = 2.0
  2. 8/3 = 2
  3. -3/2 = -1

Modulo operator (%)

This operator gives the remainder when ‘a’ is divided by ‘b’ and is represented as a%b.

This operator can only be applied on integers. To get the remainder on real numbers there is a separate mathematical function called “fmod”.

There are some rules with respect to modulo operator,

  1. Always take the sign of numerator (‘a’)
  2. a%b = a, if |a|<|b|

Let us understand these by some examples.


r = 8 % 3

r  = 2

We get remainder as 2, when 8 is divided by 3.


r = 4 % 4

r  = 0

We get remainder as 0, when 4 is divided by 4.



r = 3 % 4

r  = 3.    (a%b = a, if |a|<|b|)


r = -7 % 4

r  = -3.    (we divide 7 by 4 and get the remainder as 3, and always take the sign of first number)


r = 7 % -4

r  = 3.    (we divide 7 by 4 and get the remainder as 3, and always take the sign of first number)


r = -7 % -4

r  = -3.    (we divide 7 by 4 and get the remainder as 3, and always take the sign of first number)


r = -4 % -7

r = -4. (Take sign of 1st number and a%b = a, if |a|<|b|)

The following expressions are invalid.

1) 2.3 % 2 (Error: since modulo can only be applied on integers)

2) 4 % 2.0 (Error: since modulo can only be applied on integers)

Scroll to Top