Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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.
Advertisements
