Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

In order to calculate POWER of given x, y.

We have probably worked out 25 something like this:


We started with the simplest case:

20 = 1

Then we multiplied the previous result by 2:

21 = 2 x 20 = 2

Then we multiplied the previous result by 2:

22 = 2 x 21 = 4

Then we multiplied the previous result by 2:

23 = 2 x 22 = 8

Then we multiplied the previous result by 2:

24 = 2 x 23 = 16

Then we multiplied the previous result by 2:

25 = 2 x 24 = 32

In general,

2n = 2 x 2n-1

Provided n is an integer >= 0.


So.
Now we can write the Java method to return the value of two
raised to some power, n.

Activity Task: Write and test your java code to solve


the above given problem.

Practical Activities:

1. Write a recursive method to get sum of all number


from 1 up to given number. E.g.
Number = 5 Result
must be sum (1+2+3+4+5)
2. Write a recursive method to get sum of all even
numbers up to given number. E.g. Number = 5 Result
must be sum (2+4)
3. Write a recursive method to get sum of all odd
numbers up to given number. E.g. Number = 5 Result
must be sum (1+3+5)
4. Write a recursive method to get subtraction of all
number from 1 up to given number. E.g.
Number = 5
Result must be sum (1-2-3-4-5)

You might also like