KnowledgeBoat Logo

Computer Applications

In order to reach the top of a pole, a monkey in his first attempt reaches to a height of 5 feet and in the subsequent jumps, he slips down by 2% of the height attained in the previous jump. The process repeats and finally the monkey reaches the top of the pole. Write a program to input height of the pole. Calculate and display the number of attempts the monkey makes to reach the top of the pole.

Java

Java Iterative Stmts

36 Likes

Answer

import java.util.Scanner;

public class KboatMonkeyPole
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter height of the pole: ");
        double poleHt = in.nextDouble();
        double jumpHt = 5.0;
        int numAttempts = 1;
        while (jumpHt < poleHt) {
            jumpHt += 5.0;
            jumpHt -= 2 * jumpHt / 100.0;
            numAttempts++;
        }
        System.out.println("Number of Attempts = " + numAttempts);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of In order to reach the top of a pole, a monkey in his first attempt reaches to a height of 5 feet and in the subsequent jumps, he slips down by 2% of the height attained in the previous jump. The process repeats and finally the monkey reaches the top of the pole. Write a program to input height of the pole. Calculate and display the number of attempts the monkey makes to reach the top of the pole.

Answered By

16 Likes


Related Questions