This is saying that the sum is 0.0 but I am not sure if it is right. Can someone let me know please. Here is the code i used:

import java.util.Scanner;

public class Lab424 {
  public static void main(String[] args) {

    int i; float sum;
    i=3;
    sum=0;
    while(i<=99)
    {
    sum=sum + ((i-2)/i);
    i=i+2;
    }
    System.out. println("Sum is " + sum);
  }
}

Recommended Answers

>>((i-2)/i);

your doing integer division. You want sum += (float)(i - 2) / i;

Jump to Post

All 2 Replies

>>((i-2)/i);

your doing integer division. You want sum += (float)(i - 2) / i;

you should use a float or double for your 'i' value.
you're dividing a value x by a value greater than x, which gives a result of
0.xxx, which can not be stored into a float variable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.