- 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 K digit number divisible by X in C++

In this tutorial, we are going to write a program that finds the largest k-digit number that is divisible by x.

Let's see the steps to solve the problem.

- Initialise the x and k.
- Find the value of pow(10, k) - 1 which is largest k-digit number.
- Now, remove the remainder value from the above value to get the largest k-digit number that is divisible by x.

## Example

Let's see the code.

#include <bits/stdc++.h> using namespace std; int answer(int x, int k) { int max = pow(10, k) - 1; return max - (max % x); } int main() { int x = 45, k = 7; cout << answer(x, k) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

9999990

## Conclusion

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

- Related Articles
- C++ Program for Largest K digit number divisible by X?
- C++ Program for the Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X
- C++ Programming for Smallest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- Python Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Largest N digit number divisible by given three numbers in C++
- Find the largest 4 digit number divisible by 16.
- Find nth number that contains the digit k or divisible by k in C++
- Largest number smaller than or equal to N divisible by K in C++
- Count n digit numbers divisible by given number in C++
- Is the digit divisible by the previous digit of the number in JavaScript
- Subarray Sums Divisible by K in C++
- Largest number less than X having at most K set bits in C++

Advertisements