Shreesha A Shreesha A - 1 month ago 6
Java Question

How do store the value of quotient while dividing two variables of dataypes BigIntger and int?

i am trying to print the value of quotient while dividing a BigInteger variable by an integer variable, but the compiler shows "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '/' first type: java.math.BigInteger second type: int"

public static void main(String[] args) {
String s;
BigInteger n, repeat, remainder;
Scanner in=new Scanner(System.in);
s=in.nextLine();
n=in.nextBigInteger();
repeat=n/s.length();
System.out.println(repeat);
}

Answer
  1. Convert the int to BigInteger.
  2. Use the BigInteger.divide method to perform the operation. (/ operand works only with primitive types.)

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class ModuloTest {
    
        public static void main(String[] args) {
            String s;
            BigInteger n, repeat, remainder;
            Scanner in = new Scanner(System.in);
            s = in.nextLine();
            n = in.nextBigInteger();
            BigInteger length = BigInteger.valueOf(s.length());
            repeat = n.divide(length);
    
            System.out.println(repeat);
        }
    
    }