Write a recursive method to do the following:
1. The nth even number can be defined as 2 + the (n-1)th even number. Write an applet using recursion that will input the even number you want and output which number that is.. ie input of 7 will get you 14 (I know, I know.. there is an easier way... but this is recursion practice.)
2. A Stinko number can be found by taking the previous Stinko number, adding 1 to it and then doubling it. The first Stinko number is 1. Write a recursive method to write the nth Stinko number. Stinko numbers.. 1, 4, 10, 22, 46, ....
3. A Smello number can be found by adding the previous 2 Smello numbers and then adding 2. Find the nth Smello number. The first 2 Smello numbers are 1. Example: 1, 1, 4, 7, 13, 22, .....
4. What is the sum of the first n numbers? that is.. 1+2+3+4+5..... If your program inputs 4, the output should be 10 (1+2+3+4). Input a 5 gets you 10. Find the recursive formula and implement it.
5. Write a recursive applet that inputs a value n and returns the factorial of that number. ie 4 factorial = 4*3*2*1
6. We will do this one in class. A Dumbo number can be found by adding the previous 2 Dumbo numbers and multiplying that sum by the one before that. The first 3 Dumbo numbers are 1, 2, and 3. The sequence is.. 1, 2, 3, 5, 16, 63, 395,...
import java.applet.Applet; import java.awt.*;
public class RecWriteList extends Applet {
TextField sizeTxt; List output; int size;
public void init() { sizeTxt = new TextField(); add(sizeTxt); output = new List(10); add(output); }
public int Moves(int s) {
if (s<4) return s; else { return (Moves(s-1)+Moves(s-2))*Moves(s-3); } }
public boolean action(Event e,Object o) { size = Integer.parseInt(sizeTxt.getText()); output.addItem("Val is " + Moves(size)); return true; } }
|