The document discusses calculating powers of numbers using recursion. It provides an example of calculating 2^n for different values of n to illustrate the recursive pattern of 2^n = 2 * 2^(n-1). It then instructs the reader to write a Java method to return the value of 2 raised to a given power n using recursion. It also provides 4 practical activity tasks involving writing recursive methods to calculate sums and differences of numbers up to a given value.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
17 views
Practical Activities:: Activity Task
The document discusses calculating powers of numbers using recursion. It provides an example of calculating 2^n for different values of n to illustrate the recursive pattern of 2^n = 2 * 2^(n-1). It then instructs the reader to write a Java method to return the value of 2 raised to a given power n using recursion. It also provides 4 practical activity tasks involving writing recursive methods to calculate sums and differences of numbers up to a given value.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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)