"WHILE" Loops

E

ericcc

"Use WHILE. Write an application that calculates the product of the
odd integers from 1 to 15 and then displays the result."

How would I do this?
 
A

acilelaure

"Use WHILE. Write an application that calculates the product of the
odd integers from 1 to 15 and then displays the result."

How would I do this?
doing your homework ;-)

int i=3;
int e=1;
while(i<=15) {
if( i%2 == 0 )
e*=i;
i++;
}
System.out.println(e);
 
R

rossum

"Use WHILE. Write an application that calculates the product of the
odd integers from 1 to 15 and then displays the result."

How would I do this?
Do you know what a while loop is? Can you write one? Were you
present when you instructor taught about while loops? If not then you
will need to read you Java book about while loops or ask your
instructor.

Do you know what "product" means in this context? If not then ask
your instructor for clarification.

Do you know what an "odd integer" is? If not then consult a maths
text or ask your instructor.

Do you nkow how to display the reesult? If not then consult your Java
book or ask your instructor.

At this point you should understand the question. You can make your
first attempt at writing the answer. Test your attempt and fix any
errors. If you find you cannot fix a problem than post your code here
and we can help you. You need to post code for two reasons: first to
show that you are willing to do the work yourself and second to let us
get a handle on how much you know so we can tailor our advice to your
level of skill.

The product of all the odd integers from 1 to 15 is 2027025, which you
will need to know when you are checking your solution.

rossum
 
M

Matt Humphrey

| > "Use WHILE. Write an application that calculates the product of the
| > odd integers from 1 to 15 and then displays the result."
| >
| > How would I do this?
| doing your homework ;-)
|
| int i=3;
| int e=1;
| while(i<=15) {
| if( i%2 == 0 )
| e*=i;
| i++;
| }
| System.out.println(e);

What an interesting, subtle answer. Because the OP doesn't understand the
problem, he'll never find the bug in it.
 
L

Lew

Matt said:
| > "Use WHILE. Write an application that calculates the product of the
| > odd integers from 1 to 15 and then displays the result."
| >
| > How would I do this?
| doing your homework ;-)
|
| int i=3;
| int e=1;
| while(i<=15) {
| if( i%2 == 0 )
| e*=i;
| i++;
| }
| System.out.println(e);

What an interesting, subtle answer. Because the OP doesn't understand the
problem, he'll never find the bug in it.

But even with that lack of understanding, if they run the example they will
quickly find out that it gives the wrong answer. They just won't know why.
 
L

Lew

rossum said:
The product of all the odd integers from 1 to 15 is 2027025, which you
will need to know when you are checking your solution.

Come on, give them credit for being able to use a calculator, at least.

BTW, there is no such thing as "WHILE" in Java. The assignment must have
meant something else than what people have been answering, perhaps some custom
idiom or module the professor provides?
 
E

ericcc

oh wow. i wasn't expecting this kind of response. all you guys are so
great, you helped me so much. i have a question though, what does
"if( i%2 != 0)" mean?
 
T

Travis James

ericcc said:
oh wow. i wasn't expecting this kind of response. all you guys are so
great, you helped me so much. i have a question though, what does
"if( i%2 != 0)" mean?
It means there's a mistake in the answer. Look up the word "modulus" and
it'll make sense. (If it doesn't, consider dropping the course.)
 
M

Matt Humphrey

| Matt Humphrey wrote:
| > | > | > "Use WHILE. Write an application that calculates the product of the
| > | > odd integers from 1 to 15 and then displays the result."
| > | >
| > | > How would I do this?
| > | doing your homework ;-)
| > |
| > | int i=3;
| > | int e=1;
| > | while(i<=15) {
| > | if( i%2 == 0 )
| > | e*=i;
| > | i++;
| > | }
| > | System.out.println(e);
| >
| > What an interesting, subtle answer. Because the OP doesn't understand
the
| > problem, he'll never find the bug in it.
|
| But even with that lack of understanding, if they run the example they
will
| quickly find out that it gives the wrong answer. They just won't know
why.

Having taught Computer Science at a university, it's my experience that the
student will simply take the output as correct without crosschecking it.
 
J

John W. Kennedy

Travis said:
It means there's a mistake in the answer. Look up the word "modulus" and
it'll make sense. (If it doesn't, consider dropping the course.)

Actually, if you reread the thread closely, I think you may find that
"ericcc" is having a bit of fun with you.
 
C

Chronic Philharmonic

Matt Humphrey said:
| Matt Humphrey wrote:
| > | > | > "Use WHILE. Write an application that calculates the product of
the
| > | > odd integers from 1 to 15 and then displays the result."
| > | >
| > | > How would I do this?
| > | doing your homework ;-)
| > |
| > | int i=3;
| > | int e=1;
| > | while(i<=15) {
| > | if( i%2 == 0 )
| > | e*=i;
| > | i++;
| > | }
| > | System.out.println(e);
| >
| > What an interesting, subtle answer. Because the OP doesn't understand
the
| > problem, he'll never find the bug in it.
|
| But even with that lack of understanding, if they run the example they
will
| quickly find out that it gives the wrong answer. They just won't know
why.

Having taught Computer Science at a university, it's my experience that
the
student will simply take the output as correct without crosschecking it.

Then they will get a grade corresponding to the effort they put into it.
 
C

Chronic Philharmonic

Lew said:
How idealistic.

Heh. Point taken, however the profs I had actually checked our work for
correctness, and graded accordingly. It has been 30 years; perhaps that is a
passé concept.
 
B

blmblm

My experience is similar. Alas.
Heh. Point taken, however the profs I had actually checked our work for
correctness, and graded accordingly. It has been 30 years; perhaps that is a
passé concept.

Putting in my two cents' worth as someone who teaches undergrad
CS courses, many of them involving programming:

I can't speak for all instructors, but some of us still try to
check students' work for correctness, and -- well, I suppose it
depends on what you mean by "grade accordingly", but certainly I
deduct points if I discover [*] that a program submitted for a grade
doesn't do what it's supposed to.

[*] Usual caveats about testing apply. I try to also at least
look at their code, but sheer volume often means I can't look
very carefully.
 
A

Andreas Leitgeb

ericcc said:
"Use WHILE. Write an application that calculates the product of the
odd integers from 1 to 15 and then displays the result."
How would I do this?

while (0) {} // use while loop
long result= 2027025 + 0; // the +0 is, so it is "calculated"
System.out.println(result);// display the result
 
D

Daniel Pitts

Andreas said:
while (0) {} // use while loop
long result= 2027025 + 0; // the +0 is, so it is "calculated"
System.out.println(result);// display the result
while(0) doesn't work in Java. You need something that evaluates to a
boolean value.
 
R

rossum

while(0) doesn't work in Java. You need something that evaluates to a
boolean value.
Shhh. When answering homework questions you must include at least one
deliberate error so that the questioner has to actually read the code
rather than just copy it. Andreas was right.

rossum
 
D

Daniel Pitts

rossum said:
Shhh. When answering homework questions you must include at least one
deliberate error so that the questioner has to actually read the code
rather than just copy it. Andreas was right.

I didn't tell them how to fix the error, now did I? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top