C Program to Find the Remainder Without Using Modulo Operator

Howdy readers, today you will learn how to write a C program to find the remainder without using the modulo operator.

In the C Programming language, the modulus operator (%) is used to find the remainder. For example, 15 % 4 => 3 where 3 is the remainder.

But in this program, we will find the remainder without the modulo operator. We will be using the following methods to find the remainder:

  1. Using Mathematics
  2. Using Repeated Subtraction

So, without any delay, let’s begin this tutorial.

C Program to Find the Remainder Without Using Modulo Operator

C Program

// C Program to Find the Remainder Without Using Modulo Operator
#include <stdio.h>
int main(){
    int a, b, rem;
    
    // Taking Input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    
    // Remainder
    rem = a - (a/b) * b;
    
    // Display Remainder
    printf("The remainder is: %d", rem);
    
    return 0;
}

Output

Enter the first number: 20
Enter the second number: 3
The remainder is: 2
C Program to Find the Remainder Without Using Modulo Operator

Explanation

    int a, b, rem;

We have created three integer variables to store the values of dividend (a), divisor (b) and the remainder (rem).

    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);

The user is asked to input the dividend and divisor. The dividend and divisor are stored in the a and b variables respectively.

    rem = a - (a/b) * b;

Now, we used the mathematical formula to find the remainder.

Dividend = (Quotient x Divisor) + Remainder

It can also be written as, 

Remainder = Dividend – (Quotient x Divisor)
rem = a – ((a/b) * b)

where, 

  • a is the dividend
  • b is the divisor
  • rem is the remainder

Note: a/b returns quotient.

The computed remainder is stored in the rem variable.

    printf("The remainder is: %d", rem);

In the end, we print the value of the remainder on the screen using the printf() function.

C Program to Find the Remainder Using Repeated Subtraction

C Program

// C Program to Find the Remainder Using Repeated Subtraction
#include <stdio.h>
int main(){
    int a, b, r;
    
    // Taking Input
    printf("Enter a dividend: ");
    scanf("%d", &a);
    printf("Enter a divisor: ");
    scanf("%d", &b);
    
    // Remainder Using Repeated Subtraction
    while (a > b){
        a = a - b;
    }
    
    // Store Remainder Value 
    r = a;
    
    // Print Remainder
    printf("The remainder is: %d", r);
    return 0;
}

Output

Enter a dividend: 10
Enter a divisor: 3
The remainder is: 1
C Program to Find the Remainder Using Repeated Subtraction

Explanation

    while (a > b){
        a = a - b;
    }

We used a while loop to find out the remainder.

Suppose the user enters a = 10 and b = 3. Then,

  • 1st While Loop Iteration:
    • while (10 > 3), the condition is True.
    • a = a – b => 10 – 3 => 7.
  • 2nd While Loop Iteration:
    • while (7 > 3), the condition is True.
    • a = a – b => 7 – 3 => 4.
  • 3rd While Loop Iteration:
    • while (4 > 3), the condition is True.
    • a = a – b => 4 – 3 => 1
  • 4th While Loop Iteration:
    • while (1 > 3), the condition is False, so the loop terminates. The value of a becomes 1.
    r = a;

The leftover value is the remainder which is stored in the r variable.

Conclusion

Today you learned how to write a C program to find the remainder without using the modulo operator (%).

You also learned how to find the remainder using

  1. Using Simple Mathematics
  2. Using Repeated Subtraction

If you have any queries, comment them down in the comment section.

Leave a Comment