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


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

Updated on: 19-Aug-2019

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements