Find the Number of Solution to Modular Equations using C++

In this article we will explain everything about what is the solution of modular equations, also we will write a program to find a number of solutions to modular equations. So here is the basic example −

Input : X = 30 Y = 2
Output : 4, 7, 14, 28
Explanation : 30 mod 4 = 2 (equals Y),
   30 mod 7 = 2 (equals Y),
   30 mod 14 = 2 (equals Y),
   30 mod 28 = 2 (equals Y)
Input : X = 30 Y = 2
Output : 4, 7, 14, 28
Explanation : 30 mod 4 = 2 (equals Y),
   30 mod 7 = 2 (equals Y),
   30 mod 14 = 2 (equals Y),
   30 mod 28 = 2 (equals Y)

As we can see in the above example, every integer is the solution that gives remainder Y upon dividing X. In this example, dividing 30 with 4, 7, 14, 28 gives the remainder two, which is equal to Y. We will solve modulus equations in this manner.

Approaches to Find Solution

We can apply a simple approach that divides X by each integer starting from 1 and checks whether it gives remainder Y, or we can divide (X - Y) with each integer and integer which divides (X - Y) but not X are the solutions. Let's write a C++ program to find a different solution to modular equations.


#include <bits/stdc++.h>
using namespace std;
int numberofdivisor(int X, int Y){
    int N = (X - Y);
    int noOfDivisors = 1;
    for (int i = 1; i <= N/2; i++) {
        // if N is divisible by i
        if ((N % i) == 0) {
            // count if integer is greater than Y
            if (i > Y)
    return noOfDivisors;
void numberofsolutions(int X, int Y){
    int noOfSolutions;
    if (X == Y)
        noOfSolutions = -1;
    if (X < Y)
        noOfSolutions = 0;
    if (X > Y)
        noOfSolutions = numberofdivisor(X, Y);
        if (noOfSolutions == -1) {
            cout << "X can take Infinitely many values"
            " greater than " << X << "\n";
    else {
        cout << "Number of solution = " << noOfSolutions;
// main function
int main(){
    int X,Y;
        cin >> X;
        cin >> Y;
    numberofsolutions(X, Y);
    return 0;


When we write 0 as an input, then the program gives an output like this −

X can take Infinitely many values greater than 0

When we put other numbers, then the above program shows output like this (here we provided 5 as an input) −

Number of solution = 2

Explanation of the Above Code

Now we will explain each function so that you can understand the program easily.

main() function

In main function we are taking value of X and Y as input and finding number of possible solutions by calling numberofsolutions() function.


This function checks whether X and Y satisfy the condition, in which X should be greater than Y because we cannot find the remainder greater than the dividend. This function calls another function numberofdivisor() and fetches the number of divisors of X, which gives remainder Y.

Numberofdivisor() function

This function finds the number of divisors of X - Y by running a loop from 1 to (X - Y)/2 and checking every integer whether it divides or not, and this integer should not divide X perfectly.


The solution to modular equations is the integer which divides X and gives remainder Y; we understand this from various examples. Equations can have some solutions, so we find these solutions by applying a simple approach.

We can write a C++ program that will calculate the solutions to modular equations. We can write the same program in other languages such as C, Java, Python, or any other programming language. Hope you find this article helpful in understanding the concept of how to find a number of solutions to modular equations.