Help with Fraction with java

0

I want to do the following algorithm:

300/15 + 350/14 - 400/13 + 450/12 - .....

That is, when the denominator is odd it adds up to the next result, if it is even, it decreases.

Here's my code, but it's giving me a different result from the calculators:

// one class needs to have a main() method
public class HelloWorld {
    // arguments are passed using the text field below this editor
    public static void main(String[] args) {
        double numerador = 300.0;
        double resultado = 0.0;
        for (int i = 15; i > 0; i--) {
            if (i % 2 == 0) {
                resultado = resultado - (numerador / i);
                System.out.println("s " + numerador);
                System.out.println(i);
                System.out.println(resultado);
            } else {
                resultado = (numerador / i) + resultado;
                System.out.println("d " + numerador);
                System.out.println(i);
                System.out.println(resultado);
            }
            numerador = numerador + 50;
        }

        System.out.println(resultado);
    }
}
    
asked by anonymous 15.09.2018 / 05:05

0 answers