Modulus or Modulo Division In C Programming Language

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

Expected Output for the Input

User Input:
Enter value of a and b for modular division
10
5

Output:
10 mod 5 = 0

Important Points To Remember About Modulo Division

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


[youtube https://www.youtube.com/watch?v=3Gz_CLMiGFQ]

YouTube Link: https://www.youtube.com/watch?v=3Gz_CLMiGFQ [Watch the Video In Full Screen.]

Source Code: 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

C Program To Find Absolute Value of a Number

Write a C program to find absolute value of a number entered through the keyboard.

Absolute Value
In mathematics, the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign. The absolute value of a number may be thought of as its distance from zero.

Note: abs() is a builtin method/function present in library/header file stdlib.h

Example For Absolute Value of a Number

1. If user enters/inputs a value of 5. Then the distance from 0 to 5 is 5 units. So the absolute value of 5 is 5. Mathematically its written as |5| = 5. Its read as Modulus 5 is 5.

2. If user enters/inputs a value of -5. Then the distance from 0 to -5 is 5 units. So the absolute value of -5 is 5. Mathematically its written as |-5| = 5. Its read as Modulus -5 is 5.

scale

Expected Output for the Input

User Input:
Enter a positive or negative number
-14

Output:
Absolute Value of -14 is 14

Video Tutorial: C Program To Find Absolute Value of a Number


[youtube https://www.youtube.com/watch?v=O8Q7YEgq7_c]

YouTube Link: https://www.youtube.com/watch?v=O8Q7YEgq7_c [Watch the Video In Full Screen.]

Source Code: C Program To Find Absolute Value of a Number

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int num;

    printf("Enter a positive or negative number\n");
    scanf("%d", &num);

    printf("Absolute Value of |%d| is %d\n", num, abs(num));

    return 0;
}

Output 1:
Enter a positive or negative number
14
Absolute Value of |14| is 14

Output 2:
Enter a positive or negative number
41
Absolute Value of |41| is 41

Output 3:
Enter a positive or negative number
-2
Absolute Value of |-2| is 2

Output 4:
Enter a positive or negative number
2
Absolute Value of |2| is 2

Output 5:
Enter a positive or negative number
-100
Absolute Value of |-100| is 100

Note: abs() works only for integer values and not for floating or double type data.

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