11_lewis_409167_labMan11

# 11_lewis_409167_labMan11 - Chapter 11: Recursion Lab...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 11: Recursion 221 Chapter 11: Recursion Lab Exercises Topics Lab Exercises Basic Recursion Computing Powers Counting and Summing Digits Base Conversion Efficient Computation of Fibonacci Numbers Recursion on Strings Palindromes Printing a String Backwards Recursion on Arrays Recursive Linear Search Recursive Binary Search A List of Employees Fractals Sierpinski Triangles Modifying the Koch Snowflake

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
222 Chapter 11: Recursion Computing Powers Computing a positive integer power of a number is easily seen as a recursive process. Consider a n : & If n = 0, a n is 1 (by definition) & If n > 0, a n is a * a n–1 File Power.java contains a main program that reads in integers base and exp and calls method power to compute base exp . Fill in the code for power to make it a recursive method to do the power computation. The comments provide guidance. // **************************************************************** // Power.java // // Reads in two integers and uses a recursive power method // to compute the first raised to the second power. // **************************************************************** import java.util.Scanner; public class Power { public static void main(String[] args) { int base, exp; int answer; Scanner scan = new Scanner(System.in); System.out.print("Welcome to the power program! "); System.out.println("Please use integers only."); //get base System.out.print("Enter the base you would like raised to a power: "); base = scan.nextInt(); //get exponent System.out.print("Enter the power you would like it raised to: "); exp = scan.nextInt(); answer = power(base,exp); System.out.println(base + " raised to the " + exp + " is " + answer); } // ------------------------------------------------- // Computes and returns base^exp // ------------------------------------------------- public static int power(int base, int exp) { int pow; //if the exponent is 0, set pow to 1 //otherwise set pow to base*base^(exp-1) //return pow } }
Chapter 11: Recursion 223 Counting and Summing Digits The problem of counting the digits in a positive integer or summing those digits can be solved recursively. For example, to count the number of digits think as follows: & If the integer is less than 10 there is only one digit (the base case). & Otherwise, the number of digits is 1 (for the units digit) plus the number of digits in the rest of the integer (what's left after the units digit is taken off). For example, the number of digits in 3278 is 1 + the number of digits in 327. The following is the recursive algorithm implemented in Java. public int numDigits (int num) { if (num < 10) return (1); // a number < 10 has only one digit else return (1 + numDigits (num / 10)); } Note that in the recursive step, the value returned is 1 (counts the units digit) + the result of the call to determine the number of digits in num / 10 . Recall that num/10 is the quotient when num is divided by 10 so it would be all the digits except the units digit.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

### Page1 / 29

11_lewis_409167_labMan11 - Chapter 11: Recursion Lab...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online