- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Largest N digit number divisible by given three numbers in C++
In this tutorial, we are going to write a program that finds the largest n-digit number that is divisible by the given three numbers.
Let's see the steps to solve the problem.
- Initialise three numbers along with n.
- Find the LCM of three numbers.
- Store the largest number with n-digits.
- If the largest number is divisible by n, then return it.
- Else check for the number obtained from subtracting remainder in the above step.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int LCM(int x, int y, int z) { int ans = ((x * y) / (__gcd(x, y))); return ((z * ans) / (__gcd(ans, z))); } int findNumber(int n, int x, int y, int z) { int lcm = LCM(x, y, z); int largestNDigitNumber = pow(10, n) - 1; int remainder = largestNDigitNumber % lcm; if (remainder == 0) { return largestNDigitNumber; } largestNDigitNumber -= remainder; if (largestNDigitNumber >= pow(10, n - 1)) { return largestNDigitNumber; } return 0; } int main() { int n = 4, x = 6, y = 7, z = 8; int result = findNumber(n, x, y, z); if (result != 0) { cout << result << endl; }else { cout << "Not possible" << endl; } return 0; }
Output
If you run the above code, then you will get the following result.
9912
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Count n digit numbers divisible by given number in C++
- Largest K digit number divisible by X in C++
- C++ Program for Largest K digit number divisible by X?
- C++ Program for the Largest K digit number divisible by X?
- Find the largest 4 digit number divisible by 16.
- How many three digit numbers are divisible by 7?
- Java Program for Largest K digit number divisible by X
- How many three digit natural numbers are divisible by 7?
- Largest Even and Odd N-digit numbers in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Find the number of all three digit natural numbers which are divisible by 9.
- Largest number smaller than or equal to N divisible by K in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Largest even digit number not greater than N in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++

Advertisements