- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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.

## Example

#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) noOfDivisors++; } } 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; }

## Output

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.

## 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.

## Conclusion

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.

- Related Articles
- C/C++ Program for Number of solutions to the Modular Equations?
- C/C++ Program for Number of solutions to Modular Equations?
- Program for Number of solutions to Modular Equations in C/C++?
- Solution of Difference Equations Using Z-Transform
- Find the repeating and the missing number using two equations in C++
- Find whether this pair of equations has a unique solution or an infinite number of solutions or no solution: $3x+5y=13$, $5x+3y=4$.
- Program to find coefficients of linear equations that has only one solution in Python
- Find the solution of the following additions using a number line:(a) $(-2) + 6$ (b) $(-6)+2$
- Modular Exponentiation (Power in Modular Arithmetic) in java
- Find the number of values of $k$ for which the system of equations $x+y=2,\ kx+y=4,\ x+ky=5$ has only one solution.
- Find modular node in a linked list in C++
- Find the value of $k$ for which the system of equations $kx+2y=5$, $3x+y=1$ has no solution.
- Find the value(s) of \( p \) for the following pair of equations:\( -x+p y=1 \) and \( p x-y=1 \),if the pair of equations has no solution.
- Program to find root of an equations using secant method in C++
- Find the number of zeroes using C++