File tree Expand file tree Collapse file tree 1 file changed +39
-1
lines changed
Expand file tree Collapse file tree 1 file changed +39
-1
lines changed Original file line number Diff line number Diff line change 1- //make changes
1+
2+ /**
3+ * (Multiply the digits in an integer) Write a program that reads an integer between 0 and 1000 and multiplies
4+ * all the digits in the integer. For example, if an integer is 932, the multiplication of all its digits is 54.
5+ * Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit.
6+ * For instance, 932 % 10 = 2 and 932 / 10 = 93.
7+ *
8+ * Author : Harmeet Matharoo
9+ * Date : May 04 2021
10+ */
11+ import java .util .Scanner ;
12+
13+ public class Multiplier {
14+ public static void main (String [] args ) {
15+
16+ Scanner input = new Scanner (System .in );
17+
18+ //Enter a number between 0 and 1000: asks for input
19+ System .out .print ("Enter a number between 0 and 1000: " );
20+ int number = input .nextInt ();
21+
22+ //The multiplication of all digits in "input" is: calculate it first
23+
24+ // extracts last digit of input
25+ int num1 = number % 10 ;
26+ // remove the extracted digit
27+ int rem1 = number / 10 ;
28+ //extract the last digit(with whats left)
29+ int num2 = rem1 % 10 ;
30+ //remove the extracted digit
31+ int rem2 = rem1 / 10 ;
32+ //calculate the multiplication
33+ int multiplication = num1 * num2 * rem2 ;
34+
35+ //print it out
36+ System .out .println ("The multiplication of all digits in " + number + " is : " + multiplication );
37+
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments