-2

I got a very simple problem that I cant figure out.

This is what i want to do :

6 * (1/(1*1) + 1/(2*2) + 1/(3*3) + … + 1/(N*N))

And this is my code attempt, that does not work.

int eingabe = 5;
double c = 0;

    for (int i = 1 ; i<=eingabe ;i++) {
         c += 1/(i*i);
    }
    c *= 6;
    System.out.println(c);

Please help me guys ! What do I have to do to make the code work?

Braj
  • 46,415
  • 5
  • 60
  • 76
Quatsch
  • 361
  • 1
  • 8
  • 29

2 Answers2

7

change 1/(i*i) to 1.0/(i*i), currently you're doing integer division

yurib
  • 8,043
  • 3
  • 30
  • 55
2
int eingabe = 5;
double c = 0;

for (int i = 1 ; i<=eingabe ;i++) {
     c += 1.0/(double)(i*i);
}
c *= 6;
System.out.println(c);
i.krivosheev
  • 387
  • 3
  • 18