Question

In JAVA Write an application that calculates the product of a series of integers that are...

In JAVA

Write an application that calculates the product of a series of integers that are passed to method product using a variable-length argument list, Test your method with several calls, each with a different number of arguments.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// This class ProductOfIntegers finds the product of series of integers that
// are passed to a method product using a variable-length argument list.

public class ProductOfIntegers
{

// A method that takes variable number of integer arguments.
// Variable length arguments are specified by three periods (...) after datatype

static void product(int... num)
{
System.out.println();

  
//num.length will give the total number of integers per call
System.out.println("Number of integers: " + num.length);
  
if ( num.length > 0) // if atleast one argument is given in the function call
{
int prod = 1;
// intitialize prod with multiplicative identity

// using for each loop to find the product of all integers
for (int n: num)
prod = prod * n;

// print the result in formatted way
System.out.print("Product of integers ( * ");
for (int n: num)
System.out.print(n + " * " );
System.out.print(") = " + prod);
System.out.println();
}

else /* if no arguments were given */
{
System.out.println("Multiplication is not possible");
}
}

  
// Driver code
public static void main(String args[])
{
// Calling the product method with different number of parameters
product(6); // one parameter
product(9 , 8); // two parameters
product(10, 2, 33, 4); // four parameters
product(1, 2, 3, 4, 5, 6); // six parameters
product(); // no parameter
}   
}

Sample output:

Add a comment
Know the answer?
Add Answer to:
In JAVA Write an application that calculates the product of a series of integers that are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT