Question: Multiples
Write a method isMultiple that
determines, for a pair of integers, whether the second integer is a multiple of
the first. The method should take two integer arguments and return true if the
second is a multiple of the first and false otherwise. [Hint: Use the remainder
operator.] Incorporate this method into an application that inputs a series of
pairs of integers (one pair at a time) and determines whether the second value
in each pair is a multiple of the first.
Solution: Multiples
//Author: Free Coder
//www.freecoder247.blogspot.com
//MultiplesFinder.java
//This program accepts two numbers and finds whether the second number is a multiple of the first.
import java.util.Scanner;
public class MultiplesFinder
{
public static void main ( String [] args)
{
//create Scanner for input
Scanner input = new Scanner (System.in);
//prompt for and input two integer numbers
System.out.print("Enter two integer values separated by spaces: ");
int num1= input.nextInt();
int num2= input.nextInt();
//determine whether the second number is a multiple of the first
Boolean result = isMultiple(num1,num2);
System.out.printf("%S ", result);
}// end of main
public static Boolean isMultiple(int num1,int num2)
{
Boolean result;
int quotient = num2%num1;
if (quotient == 0)
result = true;
else
result = false;
return result;
}// end method isMultiple
}//end class
//This program accepts two numbers and finds whether the second number is a multiple of the first.
import java.util.Scanner;
public class MultiplesFinder
{
public static void main ( String [] args)
{
//create Scanner for input
Scanner input = new Scanner (System.in);
//prompt for and input two integer numbers
System.out.print("Enter two integer values separated by spaces: ");
int num1= input.nextInt();
int num2= input.nextInt();
//determine whether the second number is a multiple of the first
Boolean result = isMultiple(num1,num2);
System.out.printf("%S ", result);
}// end of main
public static Boolean isMultiple(int num1,int num2)
{
Boolean result;
int quotient = num2%num1;
if (quotient == 0)
result = true;
else
result = false;
return result;
}// end method isMultiple
}//end class
No comments:
Post a Comment