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...

You can click ____ in the Create New section of the Welcome screen to open the New Document dialog box.

A. New B. More C. Style D. Document

Computer Science & Information Technology

NUR is based on the idea that ________.

a) a page that has not been recently used is likely to be used in the near future b) a page that has not been recently used is not likely to be used in the near future c) a page that has been recently used is not likely to be used in the near future d) none of the above

Computer Science & Information Technology

A(n) ____________________ statement names a variable and specifies the data type that can be stored in it.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

??It is possible to record audio conversations with OneNote if your computer or device has a camera and/or a microphone.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology