Today lets learn about Modulus or Modulo or Modular Division in C programming language.
Division Example
10 / 5 = 2 (quotient)
Modulo Division Example
10 % 5 = 0 (remainder)
Note:
Division operation returns Quotient.
Modulo Division operation returns Remainder.
quotient = dividend / divisor;
remainder = dividend % divisor;
Related Read:
Using Scanf in C Program
Basic Arithmetic Operations In C
Page Contents
User Input:
Enter value of a and b for modular division
10
5
Output:
10 mod 5 = 0
1. Modulo Division can only be used with Integers and not with Floating point numbers.
2. When numerator is smaller than denominator, then numerator itself is returned as remainder.
3. When numerator is greater than denominator, then remainder is returned.
4. In modulo Division operation, remainder will always have the same sign as that of the dividend or numerator.
5. Symbol of modular division is %.
Video Tutorial: Modulus or Modulo Division In C Programming Language
#include<stdio.h> int main() { int a, b; printf("Enter value of a and b for modular division\n"); scanf("%d%d", &a, &b); printf("%d mod %d = %d\n", a, b, (a%b)); return 0; }
Output 1:
Enter value of a and b for modular division
10
3
10 mod 3 = 1
Output 2:
Enter value of a and b for modular division
-10
2
-10 mod 2 = 0
Output 3:
Enter value of a and b for modular division
-10
3
-10 mod 3 = -1
Output 4:
Enter value of a and b for modular division
10
-3
10 mod -3 = 1
Output 5:
Enter value of a and b for modular division
-10
-3
-10 mod -3 = -1
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert