# Iterations - finding the same value for 2 variables

Arjun Bajaj

Greenhorn

Posts: 13

posted 6 years ago

Hi everyone.....

I'm trying to make a program which can calculate the turning effects of rod.....

i have made this :

but i want to calculate the value which the program will get same for both

i tried this but i didn't work.... :

this tells me that i need a boolean rather than an int but it doesn't even allow casting...

so what should i do to get that value which is same for both the variables???

please help......its my physics homework.......

I'm trying to make a program which can calculate the turning effects of rod.....

i have made this :

but i want to calculate the value which the program will get same for both

`moment`and`moment2`i tried this but i didn't work.... :

this tells me that i need a boolean rather than an int but it doesn't even allow casting...

so what should i do to get that value which is same for both the variables???

please help......its my physics homework.......

Joanne Neal

Rancher

Posts: 3742

16

Arjun Bajaj

Greenhorn

Posts: 13

posted 6 years ago

Let's iterate a bit manually, shall we?

force == 2, distance == 18 => moment == 36

force2 == 2, distance2 == 9 => moment2 == 18

force == 3, distance == 18 => moment == 54

force2 == 3, distance == 9 => moment == 27

force == 4, distance == 18 => moment == 72

force2 == 4, distance == 9 => moment == 36

As you see, the way your code works now moment will always be twice as large as moment2.

So you want to find a moment and moment2 that are equal? Then you should increment either force or force2:

I know find that moment == 36 will break the loop.

If you don't want that break in there you can increase both force and force2 instead. You then get all multiples of 18 except for 0 and 18 itself (since moment will always be larger than 18).

force == 2, distance == 18 => moment == 36

force2 == 2, distance2 == 9 => moment2 == 18

force == 3, distance == 18 => moment == 54

force2 == 3, distance == 9 => moment == 27

force == 4, distance == 18 => moment == 72

force2 == 4, distance == 9 => moment == 36

As you see, the way your code works now moment will always be twice as large as moment2.

So you want to find a moment and moment2 that are equal? Then you should increment either force or force2:

I know find that moment == 36 will break the loop.

If you don't want that break in there you can increase both force and force2 instead. You then get all multiples of 18 except for 0 and 18 itself (since moment will always be larger than 18).

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Arjun Bajaj

Greenhorn

Posts: 13

posted 6 years ago

hey thanks,

joanne neal, david newton and rob prime for answering.......

hey rob thanks......

i finally solved the problem using your suggestion.......

i just removed the break; method.....and in place of that i put

and i added a limit to the while condition and then i got like 100,000 values where those objects can be balanced......

thnx a lot everyone.....

joanne neal, david newton and rob prime for answering.......

hey rob thanks......

i finally solved the problem using your suggestion.......

i just removed the break; method.....and in place of that i put

**force++**and i added a limit to the while condition and then i got like 100,000 values where those objects can be balanced......

thnx a lot everyone.....

Consider Paul's rocket mass heater. |