Write a Java program that first reads a positive integer fro

Write a Java program that first reads a positive integer from the user- let\'s call it howMany. Then the program reads howMany pairs of integers - let\'s call them n1 and n2. For each pair, the program determines if the first component in the pair is a multiple of the second component, in other words, if n1 is a multiple of n2. For example if howMany is 5, your program will read 5 pairs of integers, and for each pair (n1, n2) it will decide if n1 is a multiple of n2.

Solution

PairInput.java

import java.util.Scanner;


public class PairInput {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the number of pairs to input: \");
       int howMany = scan.nextInt();
       int n1[] = new int[howMany];
       int n2[] = new int[howMany];
       for(int i=0; i<howMany; i++){
           System.out.println(\"Enter the pair: \");
           n1[i]= scan.nextInt();
           n2[i]= scan.nextInt();
       }
       for(int i=0; i<howMany; i++){
           System.out.println(\"(\"+n1[i]+\", \"+n2[i]+\"): \"+n1[i]+\" is a multiple of \"+n2[i]+\" \"+(n1[i] % n2[i] == 0));
       }
   }

}

Output:

Enter the number of pairs to input:
5
Enter the pair:
4 2
Enter the pair:
6 5
Enter the pair:
8 7
Enter the pair:
9 3
Enter the pair:
10 5
(4, 2): 4 is a multiple of 2 true
(6, 5): 6 is a multiple of 5 false
(8, 7): 8 is a multiple of 7 false
(9, 3): 9 is a multiple of 3 true
(10, 5): 10 is a multiple of 5 true

Write a Java program that first reads a positive integer from the user- let\'s call it howMany. Then the program reads howMany pairs of integers - let\'s call t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site