Reputation: 1
So the code here is to take an integer and will multiply each of its digits, for example if I put in 4321
it would do 4*3*2*1
and come out to be 24
. The code works and everything, my question is, can someone explain how this loop works to me. Because I used basically a skeleton to make this code, but can someone walk me through how that modulus works along with the *=
and /=
?
import java.util.Scanner;
public class Multiplier {
public static void main(String[] args) {
int num, product;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer to be multiplied: ");
num = scan.nextInt();
product = 1;
while (num > 0)
{
product *= (num%10);
num/=10;
}
System.out.println("The Product of the Digits is: " +product);
}
}
Upvotes: 0
Views: 102
Reputation: 181
There are a few things you should know before we get to the loop:
The operator *=
sets the value to the value multiplied by the number specified. *=
is the shorthand notation of
number = number * number;
For example,
int value = 1;
value *= 3;
System.out.println(value);
would output 3, since the shorthand notation is the same as
int value = 1;
value = value * 3;
System.out.println(value);
The operator /=
sets the value to the value divided by the number specified. /=
is the shorthand notation of
number = number / number;
To reiterate, the line above is the same as
number /= number;
The modulus operator %
sets the value to the remainder after division of the value by a number specified.
For example,
System.out.println(4 % 3);
Will output 1, since 4 % 3 == 1 R 1
(the first one means we could divide into the number once, the R stands for remainder, and the last one is the actual result of 4%3
Think about the following scenario...
You want to see how much money you will have left if you:
In this scenario, the remainder would be $4 after paying all 10 people. This scenario is the same concept as
int remainder = 1004 % 100;
System.out.println(remainder);
We know 100 would go into 1000 ten times, and the final value of remainder would be 4
, which is what would be printed to console.
Okay. Now that you understand what those operands do, look at the while loop.
// while our input number is greater than 0
while( num > 0 ) {
// multiply the product by the remainder of number divided by ten
// (this cuts off the right-most digit of the input number
// and multiplies it to the product)
product *= (num%10);
// what actually sets our input number to number / 10.
num /= 10;
This loop continues until all digits have been "cut off" and multiplied to our product
Upvotes: 1
Reputation: 3011
%
"yields the remainder of its operands from an implied division" (Remainder Operator)*=
will update the variable to the product of the two operands/=
will update the value to the quotient of the two operandsBoth *=
and /=
are what is known as Compound Assignment Operators. Here is Java's definition of how they work
First, the left-hand operand is evaluated to produce a variable. The value of the left-hand operand is saved and then the right-hand operand is evaluated. The saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. The result of the binary operation is converted to the type of the left-hand variable.
Here are the steps broken up a little more clearly
class Operators
{
static int product = 1;
static int num = 25;
public static void main(String[] args)
{
System.out.println("------ [BEFORE MANIPULATION] ------");
System.out.println("product: " + product);
System.out.println("num: " + num);
System.out.println("-----------------------------------");
int remainder = num % 10; // remainder is equal to 5 (25 / 10 = 2.5)
product *= remainder; // product is equal to 5 (5 * 1)
num /= 10; // num is equal to 2 (25 / 10 = 2.5)
System.out.println("------ [AFTER MANIPULATION] ------");
System.out.println("remainder: " + remainder);
System.out.println("product: " + product);
System.out.println("num: " + num);
System.out.println("-----------------------------------");
}
}
Upvotes: 0
Reputation: 304
product *= (num % 10);
is equivalent to:
product = product * (num % 10);
and num /= 10;
is num = num/10;
So here, num % 10 helps to get digit one by one from right to left, and num/10 helps to set number to it's one tenth of present value, until number becomes 0.
Upvotes: 0
Reputation: 129
import java.util.Scanner;
public class Multiply {
public static void main(String[] args) {
int num, product;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an Integer to be multiplied: ");
num = scan.nextInt();
product = 1;
while (num > 0) {
// The notation variable += 2 is a common short hand for operations where it says that variable = variable + 2;
// The below line could also be written product = product * (num % 10);
product *= (num % 10);
// The way modulus (the % operator) works is it returns the remainder of division
// For example, on the first run of this program the code above would go like so:
// product = 1 * (4321 % 10)
// product = 1 * (1)
// We get a one as the remainder because we are working with integers so when we divide 4321 by
// 10 we get 432 with a remainder of 1. The next loop will result in 432 % 10 which will give you a remainder of 2. So on and so fourth.
// The below line could also be written num = num / 10;
num /= 10;
}
System.out.println("The Product of the Digits is: " +product);
}
}
Upvotes: 0
Reputation: 75
Modulo works by dividing the num by whatever is after the % and then the result is the remainder. For example, a % (mod) b is a / b's remainder.
So in your case: product = product * (num % 10).
Product is 1, times, 1234 % 10, or the remainder of 1234 / 10 which is 4.
num /= 10 is shorthand for num = num / 10, same for the *=, +=, -= ETC...
So then it divides 1234 by 10, and it is an integer, it truncates the 123.4 to 123 and loops
Upvotes: 0
Reputation: 67
You basically want to know the meaning of % operator and what does a while loop do.
%(Modulus Operator) would give the remainder when an integer is divided by the number succeeding this symbol. In this case it is 10.
Take an example of an integer. Lets say I take 6543. What would this code do?
When the loop starts, remainder when 6543 is divided by 10 is 3. So product = product*3. Therefore, product = 3. Now, 6543 is divided by 10. As num is an integer, 6543/10=654.
Now loop would start again for this number. Remainder this time would be 4 and num would change to 65. Product becomes 12.
It would go the same manner ahead. When num becomes 0, the loop would stop.
Upvotes: 0