- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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) 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; }

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

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

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.

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.

- Related Questions & Answers
- 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++?
- Program to find coefficients of linear equations that has only one solution in Python
- C program to find the solution of linear equation
- Program to find root of an equations using secant method in C++
- Find the Number of Stopping Stations using C++
- Find the Number of Ways to Pair People using C++
- Program to find number of solutions for given equations with four parameters in Python
- Find the Pell Number using C++
- Satisfiability of Equality Equations in C++
- Find modular node in a linked list in C++
- Find the Number of Substrings of a String using C++
- Find n positive integers that satisfy the given equations in C++
- Find the Pentagonal Pyramidal Number using C++

Advertisements