#507 Multiplication Without the Multiplication Operator

Companies:
Sberbank
Themes:
JavaJava BasicsPractice

Implement multiplication without using the multiplication operator:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        System.out.println(multiply(3, 4) + ", Expected 12");
        System.out.println(multiply(-2, 3) + ", Expected -6");   // Expected -6
        System.out.println(multiply(2, -3) + ", Expected -6");   // Expected -6
        System.out.println(multiply(-3, -4) + ", Expected 12");   // Expected 12
        System.out.println(multiply(1, 0)  + ", Expected 0");     // Expected 0
        System.out.println(multiply(0, 0)  + ", Expected 0");     // Expected 0
        System.out.println(multiply(0, 5) + ", Expected 0");     // Expected 0
        System.out.println(multiply(Integer.MAX_VALUE, 1)  + ", Expected " + Integer.MAX_VALUE); // Maximum-value check
        System.out.println(multiply(Integer.MIN_VALUE, -1)   + ", Expected " + Integer.MIN_VALUE); // Minimum-value check
    }

    public static int multiply(int a, int b) {
        // TODO
        return a * b; // only for compilation
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in