Write a method called power the computes xn where x and n and positive integers. The method has two integer parameters and returns a value of type long.

What will be an ideal response?

/** x and n are nonnegative integers */
public long power(int x, int n)
{
long result = 1;
/** check for positive numbers */
if((x >= 0) && (n >= 0))
{
/** raise x to the nth power */
for(int i = n; i > 0; i--)
result *= x;
}
else
{
result = 0;
System.out.println("Fatal error.......positive integers required!");
}
return result;
}

Computer Science & Information Technology

You might also like to view...

When using Windows 10, ________ enables you to manage your files and folders by showing the location and contents of every drive, folder, and file on your computer

A) Finder B) Internet Explorer C) System D) File Explorer

Computer Science & Information Technology

A ________ is a more efficient way to start a report, although Design view does offer you more control as you create your report

A) table B) design view C) report wizard D) form

Computer Science & Information Technology