-1

I am brand new to coding and I am having a lot of trouble just getting this to work:

Declare 2 integer variables. Prompt the user for two numbers. Store the values into variables using the Scanner object. If the second number is a multiple of the first number, display " is a multiple of ". Otherwise display " is not a multiple of "

I have this code below then i am lost:

int number1, number2;
System.out.println("Enter a number:");
number1 = keyboard.nextInt();
System.out.println("Enter a Number:");
number2 = keyboard.nextInt();

4 Answers4

0

Modulus is the remainder of dividing 2 numbers. If number2 is a multiple of number1, dividing them will have a remainder 0, otherwise not.

package test;

import java.util.Scanner;

public class MyTest {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int number1, number2;
        System.out.println("Enter a number:");
        number1 = keyboard.nextInt();
        System.out.println("Enter a Number:");
        number2 = keyboard.nextInt();

        int mod = number2 % number1;

        if (mod == 0) {
            System.out.println(number2 + " is a multiple of " + number1);
        } else { 
            System.out.println(number2 + " is not a multiple of " + number1);
        }
    }

}
Ari Singh
  • 1,228
  • 7
  • 12
0

Modulus simply gives you the remainder of the division of two numbers. For eg : 4/3 gives the remainder as 1 similarly in programmatic way you can try 4%3 which two will give the same response. Added a python code snippet below :

a = int(input("Enter first no."))
b = int(input("Enter second no."))
print("Modulus operation a%b gives : ", (a % b))
print("Is ", a, " multiple of ", b)
print((a%b) == 0)
anurag0510
  • 763
  • 1
  • 8
  • 17
0

If number1 is a multiple of number2 than it will devide number2 with remainder 0, for ex if 4 is multiple of 2 than 4%2==0, is true , here % is modulus operator that returns remainter of the division of two numbers :::

In your case if number1 is multiple of number2 than Number1%number2==0 (must);

Than your program should be like this:

If(number1%number2==0)
{
System.out.println("number1 is a multiple of number 2");
 }
Else
 System.out.println("not a multiple");
0

i am lost

Suppose you are in a competition to eat chocolates and the rule is:

Eat three chocolates at the same time

If you are given:

1 chocolate,  you will not eat, and 1 chocolate  will be left
2 chocolates, you will not eat, and 2 chocolates will be left
3 chocolates, you eat once,     and 0 chocolates will be left
4 chocolates, you eat once,     and 1 chocolate  will be left
5 chocolates, you eat once,     and 2 chocolates will be left
6 chocolates, you eat twice,    and 0 chocolates will be left
7 chocolates, you eat twice,    and 1 chocolate  will be left
...
100 chocolates, you eat 33 times, 1 chocolate will be left
...
1502 chocolates, you eat 500 times, 2 chocolates will be left

The number of chocolates left each time is called Modulus is Mathematics, a powerful operation in computers.

As you can tell, you will always be left with less than 3 chocolates (0 or 1 or 2) anytime.

Hence, x modulus y is always less than y in the range 0 to y-1.

In programming, the modulus operator is %.

So, if we write the way you ate chocolates using the modulus operator %, it looks like:

1 % 3  =  1
2 % 3  =  2
3 % 3  =  0
4 % 3  =  1
5 % 3  =  2
6 % 3  =  0
7 % 3  =  1
...
100 % 3  =  1
...
1502 % 3  =  2

Now tell me, are you lost?

Vikram Hosakote
  • 3,528
  • 12
  • 23