English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i need one more thing from somone out there. here's the problem:

write a method using for loops that reads an integer from the user; make sure the number is greater than 20. divide the integer by 2 and print out the result, keep repeating the division by 2 until the remainder of the division is 1 or 0. for example if the user enters 10, you should print something similar to:

10/2=5
5/2=2
2/2=1
Finished dividing


thanks alot i' greatly appreciate it

2007-03-14 04:21:17 · 3 answers · asked by laxplayer2604 1 in Computers & Internet Programming & Design

3 answers

i hope this is not hw, here is the code-- You are welcome. I tested it and it works! -- You need to import java.io.*;

public class Dividing {

/** Creates a new instance of Dividing */
public Dividing() {
}
public static void main(String args[]){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number : ");
int num = Integer.parseInt(br.readLine());

for (int i=0; i<=num; i++){
System.out.println(num + " / 2 = " +num/2);
num= num/2;
}
if((num %2 == 1) || (num %2== 0)){
System.out.println("Finished dividing!");
}
}
catch(Exception e){}
}

}

2007-03-14 08:50:39 · answer #1 · answered by Anonymous · 0 0

START of Method Display (Take NumberFromUserInput as parameter)
START If NumberFromUserInput is greater than 20 then
START Loop until NumberFromUserInput equals 1 or 0
NumberFromUserInput = NumberFromUserInput divided by 2
Output NumberFromUserInput to the Screen
START If NumberFromUserInput equals 1 or 0 then
EXIT
END If
END Loop
END If
END of Method Display

Work out your mind and translate this Pseudo-Code into Java Code

2007-03-14 11:34:36 · answer #2 · answered by Coosa 2 · 0 0

it's better to use the 10*8 variable.

2007-03-14 11:25:19 · answer #3 · answered by FRENCH FRIES 2 · 0 0

fedest.com, questions and answers