Write a C program to enter a number and find modular division operation by 2, 3 & 4 and display the remainders.

This program is a good example of how arithmetic modulus operator works in C language.

The modulo operator which is also known as modulus operator (denoted by %), is an arithmetic operator.

It is a binary operator which means it requires two operands to work. It produces the remainder of integer division. In simple words, if you need remainder instead of quotient then find modular division.

For example:
If you perform 23 modulo 2 then it will result 1, which is the remainder. It resulted 1 because 2 does not completely divides 23.

23 % 2 = 1 (remainder)

But, if you find 22 modulo 2 then it will result 0 because 2 completely divides 22.

22 % 2 = 0 (remainder)

Tutorial πŸ“Ί

Watch the tutorial and learn how to solve this problem. πŸ˜‰

Steps πŸͺœ

To find modular division by 2, 3 & 4

  1. Display a message to the user and accept an integer. Store the accepted value in a variable ‘num‘. Since, we can display the results directly using printf statements that is why only one variable is enough.
  2. Write three printf() statements to display the modulo operation by 2, 3 and 4 respectively. That’s it. 😎

Flow Chart 🌻

Check how to draw flow chart for this program.

flow - charts Find Modular Division by 2, 3 and 4

Code πŸ’»

#include <stdio.h>
#include <conio.h>

void main()
{
  int num = 0;
  clrscr();
  // Display a message and accept a number
  printf("Please enter the number: ");
  scanf("%d", &num);
  // Display the entered value
  printf("\n\nYou have entered: %d\n", num);
  // Find modular division and display the results
  printf("\nModular division by 2 : %d", num % 2);
  printf("\nModular division by 3 : %d", num % 3);
  printf("\nModular division by 4 : %d", num % 4);
  getch();
}

Example 😍

Let’s assume the user enters the number 7. Now, 7 modulo 2 results in 1, where 1 is the remainder. Same as 7 modulo 3 results in 1 but 7 modulo 4 results in 3.

The output will be like:

You have entered: 7

Modular division by 2 : 1 
Modular division by 3 : 1 
Modular division by 4 : 3

Let’s take another example where the user enters the number 26. Now, 26 modulo 2 results in 0, where 0 is the remainder. But, 26 modulo 3 results in 2 because if you divide 26 by 3 there will be 2 remainder. Again, 26 modulo 4 results in 2.

The output will be like:

You have entered: 26

Modular division by 2 : 0 
Modular division by 3 : 2 
Modular division by 4 : 2

This is how we perform the modulo operation by 2, 3 and 4 in C language. 😎
For more C programs, click here.

Categorized in: