Rolling Logs (Pycharm 3) Write a function another one() that takes the following
ID: 3585661 • Letter: R
Question
Rolling Logs (Pycharm 3)
Write a function another one() that takes the following arguments, in this order:
1. integer: a positive integer
2. divisor: any integer, including zero
Your function should return the number of times the given integer can be divided by the divisor until the quotient is less than 1. That is, divide integer by divisor, add one to a count, and replace integer with the quotient. For instance, suppose integer is 13 and divisor is 2. The following set of divisions will be performed: 13 ÷ 2 = 6 6 ÷ 2 = 3 3 ÷ 2 = 1 1 ÷ 2 = 0. In the case, the function would return 4.
If divisor equals 0, the function simply returns 0.
Examples:
Function Call
Return Value
another one(123, 2)
7
another one(512, 3)
6
another one(132, 5)
4
Function Call
Return Value
another one(123, 2)
7
another one(512, 3)
6
another one(132, 5)
4
Explanation / Answer
package testPackage;
import java.io.*;
public class anotherOne //Main Class
{
public static void main(String[] arg) throws Exception //Main Function
{
int number, count=1, divisor,quotient =0; // Declaring the variables
InputStreamReader h= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(h);
System.out.println("Enter the Number ");
number= Integer.parseInt(br.readLine()); // Taking the Integer from User
System.out.println("Entered Number is "+number); // Displaying the Integer taken on console
InputStreamReader h1= new InputStreamReader(System.in);
BufferedReader br1=new BufferedReader(h1);
System.out.println("Enter the Divisor ");
divisor= Integer.parseInt(br1.readLine()); // Taking the Divisor from User
System.out.println("Entered Divisor is "+divisor); // Displaying the divisor value taken on Console
if(divisor!=0) //Checking if divisor is not equal to Zero
{
do
{
quotient =number/divisor;
number=quotient ;
count=count+1;
}
while
(number>=divisor);
System.out.println("Total Count is "+count); // Displaying the count Integer can be divided with divisor
}
else
{
System.out.println("Function is returing value as 0"); // If divisor is Zero, Function is supposed to return value as 0
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.