Java Program for Smallest K digit number divisible by X

JavaServer Side ProgrammingProgramming

To find the smallest K digit number divisible by X, the Java code is as follows −

Example

 Live Demo

import java.io.*;
import java.lang.*;
public class Demo{
   public static double smallest_k(double x_val, double k_val){
      double val = 10;
      double MIN = Math.pow(val, k_val - 1);
      if (MIN % x_val == 0)
      return (MIN);
      else
      return ((MIN + x_val) - ((MIN + x_val) % x_val));
   }
   public static void main(String[] args){
      double x_val = 76;
      double k_val = 3;
      System.out.println("The smallest k digit number divisible by x is ");
      System.out.println((int)smallest_k(x_val, k_val));
   }
}

Output

The smallest k digit number divisible by x is
152

A class named Demo contains a function anmed ‘smallest_k’. This function returns the minimum number of digits of ‘k’ that completely divide the number ‘x’. In the main function, a value for ‘x’ and ‘k’ is defined. The function is called with these values and the relevant message is displayed on the console.

raja
Published on 08-Jul-2020 11:21:13
Advertisements