SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Java Primer
Exercise:
Exercises
Question:3 | ISBN:9781118771334 | Edition: 6

Question

Write a short Java method, isMultiple, that takes two long values, n and m, and returns true if and only if n is a multiple of m, that is, n = mi for some integer i.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package java_problems_datastructures;

import java.util.Scanner;

public class CheckMultiple {
	
	// main method or driver method
	public static void main(String[] args) {
		
		
		// calling the method
		isMultiple();
	}

	// this method will check whether n is multiple of m
	private static void isMultiple() throws ArithmeticException {

		Scanner input = new Scanner(System.in);

		int reminder;
        //  reads two values from the input
		int value1 = input.nextInt();
		int value2 = input.nextInt();

		reminder = value1 % value2;
        
		
		if (value2 == 0 || value1 == 0 || reminder != 0) {
			System.out.println("entered values either zero;;   OR " + value1 + " not multiple of " + value2);

		} else {

			System.out.println(value1 + " is Multiple of " + value2);

		}
		// close input stream
		input.close();

	}

}

 

0 0

Discussions

njorogemaina

We need to create a method that takes two parameters, n, and m. This method will return a boolean. True if n divides m and false otherwise.

public class checkMultiple {

    static boolean isMultiple(long n, long m) {
        return (n % m == 0);
    }
    public static void main(String[] args) {
        System.out.println(isMultiple(10, 5)); // True
    }
}

Post the discussion to improve the above solution.