In this question, you are expected to get multiple integer numbers from the user
ID: 3818147 • Letter: I
Question
In this question, you are expected to get multiple integer numbers from the user and multiply them. The user can enter 2 to 10 numbers (operands) to be multiplied. The program should check whether the number of operands is in this range using a do-while loop. Any number outside of this range is not accepted (see sample outputs) and program continues to ask the user to enter the number of operands until an acceptable number of operands is entered. Then, the program asks the numbers in order using for-loop and finally prints a message which shows the entered the numbers and their multiplication (see sample outputs). Please be consistent with the given output format. Enter the number of operands (in range 2-10): Enter the number of operands (In range 2-10): Enter the number of operands (In range 2-10): Enter number 1: Enter number 2: Enter number 3: Multiplication of numbers " 23 10 5 " is: 1150 Enter the number of operands (In range 2-10): Enter number 1: Enter number 2: Enter number 3: Enter number 4: Enter number 5: Multiplication of numbers " 12 1 3 8 23 " is; 6624Explanation / Answer
import java.util.Scanner;
public class HelloWorld
{
public static void main(String []args)
{
int i,n,num,mul; //initialisation of variables
mul=1;
String s="";
Scanner sc=new Scanner(System.in);
do
{
System.out.println("Enter the number of operands (in range 2-10): "); //accepting number of operand from user
n=sc.nextInt();
}while(n<2); // check if number of operand less than 2
for(i=1;i<=n;i++)
{
System.out.println("Enter number "+i+":");
num=sc.nextInt();
s=s+num+" ";
mul=mul*num; //calculating the result
}
System.out.println("Multiplication of numbers "+s+" :"+mul); //printing the result
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.