• March 20th, 2013, 04:14 AM
iamgonge
hello, Im new to Java and am taking a class. Ive been working on this problem for over 4 hours now and am stuck. this is the problem:

If you divide 1 by 2, you get 0.5. If you divide it again by 2, you get 0.25. Write a
program using “do…while” loop to calculate and output the number of times you have to
divide 1 by 2 to get a value less than one ten-thousands (0.0001)..

Ive tried several ways but nothing is working for me. this is my current code:

public class Divide {
public static void main(String[] args) {
double n =1;
double Final=NumR(n);
System.out.println(Final);
}
public static double NumR(double z){
double x=2;
double i =z;
double total;

do{
i =i/x;

i++;

return i;}
while(total >=.0001);

}}

it doesnt work, Im stuck. How do i return the value of the division so that i can divide it again?

--- Update ---

i think i solved it please check:
public class Divide {
public static void main(String[] args) {

double x = 1;
double count = 0;
double tempNum = x / 2;

while (tempNum >= .0001){
tempNum = tempNum / 2;
count++;

}
System.out.println("The number " + x + " is divisible by two " + count + " times and equals " + tempNum);
}
}

one issue how do i turn it into a do while loop????

--- Update ---

public class Divide {
public static void main(String[] args) {

double x = 1;
double count = 0;
double tempNum = x ;

do {
tempNum = tempNum / 2;
count++;
}
while (tempNum >= .0001);

System.out.println("The number " + x + " is divisible by two " + count + " times and equals " + tempNum);
}
}

final code? does this look right?
• March 20th, 2013, 06:41 AM
Chris.Brown.SPE