How to alternate plus and minus signs in java?

So my code is:

Code :

class series {
public static void main(String args[]){
int numerator, denominator;
numerator = 1;
denominator = 1;
System.out.print(numerator);
for(int counter=0;counter<=198;counter++){
denominator+=1;
System.out.print(" + "+numerator+"/"+denominator+" ");
}
}
}

So far it prints out this:

1 + 1/2 + 1/3 + 1/4 + 1/5...

but I want it to print out like this:

1 - 1/2 + 1/3 - 1/4 + 1/5...

How would I do it?

Re: How to alternate plus and minus signs in java?

The easiest way? Use mod. An integer mod 2 will result in either 0 or 1 (depending on whether the number is odd or even). Then you just need an if statement where:

Code java:

if(someNumber % 2 == 0)
print plus sign
else
print minus sign

Or whichever way you want. You get the idea.

**Btw, "mod" is indicated by %

Re: How to alternate plus and minus signs in java?

Code :

class series {
public static void main(String args[]){
int numerator, denominator;
numerator = 1;
denominator = 1;
System.out.print(numerator);
for(int counter=0;counter<=198;counter++){
denominator+=1;
System.out.print(" + "+numerator+"/"+denominator+" ");
if(numerator % 2 == 0){
System.out.print("+");
}else{
System.out.print("-");
}
}
}
}

But now the output is wrong:

1 + 1/2 - + 1/3 - + 1/4 - + 1/5...

Re: How to alternate plus and minus signs in java?

Well, first of all, you want to take out where you add the plus symbol in your first print statement (the one where you print the numerator and the denominator). Second, your numerator doesn't change, so that is not the number you want to mod. Instead, you want to mod either the counter or the denominator (whichever makes more sense for your problem). Lastly, you want the if/else statement before you print the numerator and denominator (since the symbol needs to come before the fraction).

Re: How to alternate plus and minus signs in java?

Ok now I have to add all those numbers together, for e.g., the sum of 1 - 1/2 + 1/3 - 1/4 + 1/5 = 0.783

Re: How to alternate plus and minus signs in java?

Oh. That's a bit more complicated. You will want a double variable located outside of your for loop. Inside your if/else statement, you would want to add or subtract the value of: numerator/denominator by the double variable outside of your for loop. This value is your total.

There is one problem with this however: numerator and denominator are both ints. So when you divide them, the result with be an int (meaning it will not contain a decimal). That's a big problem. You want your division to be a double, not an int. We can get a result to be a double only if at least one of the values are doubles. There are two ways of handling this:

1. Multiply either numerator or denominator by 1.0 before dividing them. Multiplying one of them before dividing will result in you using the value of the int times 1.0 in the division operation. By multiplying by 1.0, you are converting that value into a double, which will yield a double value after dividing. This would look like this: ((1.0*numerator)/denominator)

Note that the decimal and the 0 in the tens place is critical. Multiplying by just 1 (no decimal) will NOT result in a double.

2. You could change either numerator or denominator to be a double instead of an int. While this would not require you to do #1's multiplication, it will result in a .0 getting printed out by your print statement. This can be fixed by casting the value to an int prior to printing it:
Code java:

System.out.println(((int)numerator)+"/"+denominator); // This example assumes numerator was changed to a double

Re: How to alternate plus and minus signs in java?

Quote:

Originally Posted by

**aussiemcgr**
Oh. That's a bit more complicated. You will want a double variable located outside of your for loop. Inside your if/else statement, you would want to add or subtract the value of: numerator/denominator by the double variable outside of your for loop. This value is your total.

There is one problem with this however: numerator and denominator are both ints. So when you divide them, the result with be an int (meaning it will not contain a decimal). That's a big problem. You want your division to be a double, not an int. We can get a result to be a double only if at least one of the values are doubles. There are two ways of handling this:

1. Multiply either numerator or denominator by 1.0 before dividing them. Multiplying one of them before dividing will result in you using the value of the int times 1.0 in the division operation. By multiplying by 1.0, you are converting that value into a double, which will yield a double value after dividing. This would look like this: ((1.0*numerator)/denominator)

Note that the decimal and the 0 in the tens place is critical. Multiplying by just 1 (no decimal) will NOT result in a double.

2. You could change either numerator or denominator to be a double instead of an int. While this would not require you to do #1's multiplication, it will result in a .0 getting printed out by your print statement. This can be fixed by casting the value to an int prior to printing it:

Code java:

System.out.println(((int)numerator)+"/"+denominator); // This example assumes numerator was changed to a double

Now could you put that in English? I'm only a beginner in java.

Re: How to alternate plus and minus signs in java?

Have you got the if statement working to alternate between + and -? If so add further code in each branch to perform the different operations.