Starting Out with Java: From Control Structures through Objects (6th Edition)
6th Edition
ISBN: 9780133957051
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 16, Problem 7AW
Write an iterative version (using a loop instead of recursion) of the factorial method shown in this chapter.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a recursive method that returns the value of N! (N factorial) using the definition given in this chapter. Explain why you would not normally use recursion to solve this problem.
Ex. 01 : Recursion
About
So far, we have learned that we can perform repetitive tasks using loops. However, another way is by creating methods that call themselves. This programming technique is called recursion
and, a method that calls itself within it's own body is called a recursive method. One use of recursion is to perform repetitive tasks instead of using loops, since some problems seem to be
solved more naturally with recursion than with loops.
To solve a problem using recursion, it is broken down into sub-problems. Each sub-problem is similar to the original problem, but smaller in size. You can apply the same approach to each
sub-problem to solve it recursively.
All recursive methods use conditional tests to either
1. stop or
2. continue the recursion.
Each recursive method has the following characteristics:
1. end/terminating case: One or more end cases to stop the recursion.
2. recursive case: reduces the problem in to smaller sub-problems, until it reaches (becomes) the end…
please explain
Chapter 16 Solutions
Starting Out with Java: From Control Structures through Objects (6th Edition)
Ch. 16.2 - It is said that a recursive algorithm has more...Ch. 16.2 - Prob. 16.2CPCh. 16.2 - What is a recursive case?Ch. 16.2 - What causes a recursive algorithm to stop calling...Ch. 16.2 - What is direct recursion? What is indirect...Ch. 16 - Prob. 1MCCh. 16 - This is the part of a problem that can be solved...Ch. 16 - This is the part of a problem that is solved with...Ch. 16 - This is when a method explicitly calls itself. a....Ch. 16 - Prob. 5MC
Ch. 16 - Prob. 6MCCh. 16 - True or False: An iterative algorithm will usually...Ch. 16 - True or False: Some problems can be solved through...Ch. 16 - True or False: It is not necessary to have a base...Ch. 16 - True or False: In the base case, a recursive...Ch. 16 - Find the error in the following program: public...Ch. 16 - Prob. 1AWCh. 16 - Prob. 2AWCh. 16 - What will the following program display? public...Ch. 16 - Prob. 4AWCh. 16 - What will the following program display? public...Ch. 16 - Convert the following iterative method to one that...Ch. 16 - Write an iterative version (using a loop instead...Ch. 16 - What is the difference between an iterative...Ch. 16 - What is a recursive algorithms base case? What is...Ch. 16 - What is the base case of each of the recursive...Ch. 16 - What type of recursive method do you think would...Ch. 16 - Which repetition approach is less efficient: a...Ch. 16 - When recursion is used to solve a problem, why...Ch. 16 - How is a problem usually reduced with a recursive...Ch. 16 - Prob. 1PCCh. 16 - isMember Method Write a recursive boolean method...Ch. 16 - String Reverser Write a recursive method that...Ch. 16 - maxElement Method Write a method named maxElement,...Ch. 16 - Palindrome Detector A palindrome is any word,...Ch. 16 - Character Counter Write a method that uses...Ch. 16 - Recursive Power Method Write a method that uses...Ch. 16 - Sum of Numbers Write a method that accepts an...Ch. 16 - Ackermarms Function Ackermanns function is a...Ch. 16 - Recursive Population Class In Programming...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Markup Write a program that asks the user to enter an items wholesale cost and its markup percentage. It should...
Starting Out with C++: Early Objects (9th Edition)
This optional Google account security feature sends you a message with a code that you must enter, in addition ...
SURVEY OF OPERATING SYSTEMS
ICA 13-22
As a reminder, the Reynolds number is discussed in Chapter 9. Dimensionless Number.
When discussing t...
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
What is an object? What is a control?
Starting Out With Visual Basic (8th Edition)
What is the purpose of the let constructs in functional languages?
Concepts Of Programming Languages
Consider a class ConcertPromoter that records the tickets sold for a performance. Before the day of the concert...
Java: An Introduction to Problem Solving and Programming (8th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Flip a fair coin repeatedly until you get two heads in a row (HH). What is the probability of getting HH in at-most N (N > 1) tosses? Can you write a recursive function and implementation to calculate the probability?arrow_forwardWrite a recursive method that will compute the number of even digits in anumber.arrow_forwardImplement the The triangle.cpp Program.This program uses recursion to calculate triangular numbers. Enter a value for the term number, n, and the program will display the value of the corresponding triangular number.arrow_forward
- Given the sequence, S2 = 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, … Write a RECURSIVE method called “sequence2” that takes a single int parameter (n) and returns the int value of the nth element of the sequence S2. You will need to determine any base cases and a recursive case that describes the listed sequence. Use the following code to test your answers to questions 3 and 4the output should print the two sequences given (S & S2): public class TestSequences { public static void main(String[] args) { for(int i = 0; i < 10; i++) { System.out.print(sequence(i) + " "); // 2, 4, 6, 12, 22, 40, 74, 136, 250, 460 } System.out.println(); for(int i = 0; i < 10; i++) { System.out.print(sequence2(i) + " "); // 1, 2, 4, 5, 7, 8, 10, 11, 13, 14 } } // *** Your method for sequence here *** // *** Your method for sequences2 here *** } // end of TestSequences classarrow_forwardWrite a recursive implementation of Euclid’s algorithm for finding the greatest common divisor (GCD) of two integers. Descriptions of this algorithm are available in algebra books and on the Web. Write a test program that calls your GCD procedure five times, using the following pairs of integers: (5,20), (24,18), (11,7), (432,226), (26,13). After each procedure call, display the GCD.arrow_forward1. Let product(n,m) be a recursive addition-subtraction method for multiplying two positive integers. Recursive cases for m = 1 and m < 1 make this method. The return value should be n plus a recursive product() call with n and m - 1. Test a Java method.arrow_forward
- Write a recursive function for int powerOfTwo (int k). The function determines the value of 2k. (Note: k is a positive integer). Example, when k=0 the function returns 1 and when k-3 the function returns 8. To show that your code is correct, give the recursive trace for powerOfTwo (2) that returns 4.arrow_forwardIt is suspected that out of a set of 64 50p coins one of the coins is fake (i.e., lighter in weight than a genuine coin). With one weighing scale. (i)Give a detailed explanation on how you would go about determining the fake coin. (ii)What is the minimum number of times you need to use the scales to weigh the coins before identifying the fake coin? (iii)Write a recursive method that returns the value of N! (N factorial). Explain why you would not normally use recursion to solve this problem.arrow_forwardplease code in python Here is another recursion example, but with less guidance. Write a function log2(x), which gives an integer approximation of the log base 2 of a positive number x, but it does so using recursion. Use a base case where you return 0 if x is 1 or less. In the general case, add 1 to the answer and divide x by 2. (This is purposely vague so you can figure it out yourself.)arrow_forward
- One-friend recursion vs iteration. 1. Your objective is to receive the tuple a1, a2,..., a and return the tuple an, an1,..., a1 that has been inverted. You will only take an element off of one end or put an element back on one end because you are being lazy. But you have friends in recursion who can assist you.Please provide the recursive code as well as a paragraph with the friend's description of the algorithm.2. Now imagine that you lack friends but have a stack. Quickly design an iterative programme to address this issue. Include loop invariants and other crucial stages that are necessary to describe an iterative method.3. Trace both of these scripts separately. On a computer, step by step compare and contrast their calculations.arrow_forwardIterative FactorialWrite an iterative version (using a loop instead of recursion) of the factorial functionshown in this chapter. Test it with a driver program.arrow_forwardHelp me in recursion. Your task is to print I am coder using recursion for 7 times In elixir programming languagearrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Computational Software for Intelligent System Design; Author: Cadence Design Systems;https://www.youtube.com/watch?v=dLXZ6bM--j0;License: Standard Youtube License