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

Having some difficulties coming up with doing the following:
1. If number inputed is even, the next number wld be half of it. the output wld show the numbers until it reaches 1.

2. need to add something at the end to count the number of times it was halved.

so far i think i got the following


public static void main(String args[])
{
do
{
int x = inputInt("enter a number");
} while (x < 1);

for (int i = 2; i ? ??; i++)

if (x % 2 == 0)
x = x/ 2;
output(x);

im thinking that a for loop has to be used in order for the numbers to halve although im not certain of what i has to be.

some help wld be appreciated thx.

2007-09-26 02:39:31 · 5 answers · asked by Shadow K 1 in Computers & Internet Programming & Design

ahhhh lol not a stupid Qs at all....its just that i wanted some guidance to how to halve numbers....wanted to figure out myself to program so that if it is an even it wld just keep on halfing...and if its an odd + wtv then end at 1 .

hope that kinda made some sense lol

2007-09-26 03:56:31 · update #1

5 answers

Try a "while" loop.

while (number is greater than one)
halve the number
end while

2007-09-26 02:48:16 · answer #1 · answered by Anonymous · 0 0

This is really a fairly simple program. You mentioned that you need to half a number until the number reaches one. You also need to output the number of times it was halved.

You'll need twoint variable.

int count = 0;
int output = 0;

get user input and assign to the variable output. Next check to make sure that output is not 0:

If (output = 0 )
print error message: Number must be greater than 0.
redisplay request for input

Now you need to begin the loop something like this:

While ( output > 1 )
{
output = output/2;
count++;

Print output
}

print count ------total number of times number halved.

I suggest checking your user input before beginning the loop, this way the check is only performed once, rather than performing the check every time through the loop. This is a better way to write as it saves computer resources.

2007-09-26 11:39:06 · answer #2 · answered by ckcool192000 3 · 0 0

do
{
int x = inputInt("enter a number");
}while (x < 1);

This code will loop until the number entered is greater than 1.
While this is happening the program will do nothing else.

Your next for or while loop should be within the above loop and not outside of it.

Not a brilliant start but if you stick with it you can improve.

2007-09-26 09:56:25 · answer #3 · answered by AnalProgrammer 7 · 0 0

So it stops if it hits an odd number too, right?

int counter = 0;
while(x > 1 && x%2 ==0)
{
x = x / 2; //you could also use "x /= 2;"
System.output.println(x);
counter++;
}

2007-09-26 09:53:45 · answer #4 · answered by Thee John Galt 3 · 0 0

I have Stupid question...

are you rounding each number? There are many cases where you will never reach 1..

say I enter 30. half would be 15, 7.5, 3.25, 1.625,.8125

2007-09-26 10:23:00 · answer #5 · answered by David K 4 · 0 0

fedest.com, questions and answers