C++ Program for the Largest K digit number divisible by X?

C++Server Side ProgrammingProgramming

Two integers X and K are given. K is the number of digit in integer number. The logic is to find largest K-digit number divisible by X.

Input: X = 30, K = 3
Output: 980

Explanation

980 is the largest three digit number divisible by 30. By taking the K in power of 10 then subtracting it with 1 will give us the largest K digit number after that we will try to get the largest no. which is divided by X.

Example

#include <iostream>
#include <math.h>
using namespace std;
int main() {
   int X = 20;
   int K = 3;
   int MAX = pow(10, K) - 1;
   cout << (MAX - (MAX % X));
}
raja
Published on 19-Aug-2019 11:31:51
Advertisements