Question

Java Write a method countStairs that, given a number of blocks, counts and returns the number...

Java

Write a method countStairs that, given a number of blocks, counts and returns the number of stairs you could build with the blocks. Stairs are built in a staircase with one block for the first step, two for the second step, three for the third step, and so on, like this picture which shows * for each block in a staircase:

*
**
***

If you had 5 blocks, you could only build 2 stairs (which requires 1+2 = 3 blocks) and not 3 stairs (which would require 1+2+3 = 6 blocks). Hint: Use a loop which counts up to calculate the total number of blocks needed for successive steps until you surpass the number of blocks given. You may only use two if statements.

---

countStairs(0)
countStairs(1)
countStairs(2)
countStairs(3)
countStairs(4)
countStairs(5)
countStairs(6)
countStairs(10)
countStairs(15)

---

What I have so far does not work for inputs: 2, 4, 5

public static int countStairs(int blocks) {
int i = 0;
int n = 1;
int count = 0;
while (i < blocks){
i += n;
n++;
}
return n - 1;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int countStairs(int blocks) {
    int n = 1;
    while (n <= blocks){
        blocks -= n;
        n++;
    }
    return n-1;
}
Add a comment
Know the answer?
Add Answer to:
Java Write a method countStairs that, given a number of blocks, counts and returns the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a recursive method that counts the number of bowling pins given the number of pins...

    Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3

  • Need help with this. Java please! Write a recursive method that counts the number of bowling...

    Need help with this. Java please! Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3

  • P4 us Background) Counts N (Counts minus Background) CountsN (Counts minu 1. 1589 2. 1602 3....

    P4 us Background) Counts N (Counts minus Background) CountsN (Counts minu 1. 1589 2. 1602 3. 196 6. 19i9 506. 1559.6 15496 443.6 1593 8. 636 9. İ601 10. 1619 1623.6 1558.6 5. 1596 and the Stahdard Deviation (o): Using the data obtained in Step P4, calculate the average count (Nv Σ[M-N.vg)2/(n-I)], i-1, 2, 3, , 10: n-10 σ i-1 Calculate (Ng)" (with the Background subtracted). How close is (N.) 2 to ơ? percentage difference. Which quantity would be a...

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

  • Here is the recursion tree for the above: I need to know the formula that counts...

    Here is the recursion tree for the above: I need to know the formula that counts the nodes in that recursion tree. An example: the recursion tree for a selection sort with an array of 4 in the worst case scenario looks like this: That is the type of solution I am looking for. Please determine the formula to count the number of nodes in the recursion tree for the insertion sort with an array size of 5 (n=5) where...

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • please write legible, will rate good 1. An insurer is combining two independent blocks of insurance....

    please write legible, will rate good 1. An insurer is combining two independent blocks of insurance. The number of claims in both blocks are represented by Poisson distributions. Let N(1) and N (2) denote the number of claims in block 1 and block 2, respectively. Define N = N(1) + N(2) Given that P(N(1) = 0) = 0.1108, P(N2) = 1) = 0.31056, P(N = 2) = 0.15394, find EN(2)].

  • In JAVA: Define a test suite method, define a test method testcube that exercises Calculation.cube() in...

    In JAVA: Define a test suite method, define a test method testcube that exercises Calculation.cube() in Calculation class, and Organize your tests in your test suite (testfindMax & testcube) Given the following code below: 6. 7. 1. public class Calculation { 2. //method that returns cube of the given number 3. public int findMax(int ac[]}{ 4. int max=0; 5. for(int į=1;i<acr.length;i++){ if(max<arc[i]). max=acc[i]; } 8. return max: 10. } 11. //method that returns cube of the given number 12. public...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT