-7

Why is my code printing the output 4 times? The answer is correct but the answer is printed 4 times instead of the desired one time.

  import java.util.*;
  import java.math.BigInteger;
  class THIRTYSEVEN
  {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args)
    {
      BigInteger a = new BigInteger("1");
      multiply(a,0,sc.nextInt());
    }
    static void multiply(BigInteger b, int loop, int power)
    {
      BigInteger result = b;
      while(loop<power)
      {
          result =  result.multiply(new BigInteger("8"));
          loop++;
          multiply(result,loop,power);
      }
      System.out.println(result);
    }
  }
Andy Turner
  • 122,430
  • 10
  • 138
  • 216
  • Step through your code with a debugger. Specifically, it's because of the `System.out.println` call in the method you call recursively. – Andy Turner May 23 '18 at 04:51

1 Answers1

1

You call multiplyonly once, but it recursively calls itself (and prints every time). You could return the result instead (and print it from main).

Thilo
  • 241,635
  • 91
  • 474
  • 626