I tried debugging but the variable y remains as 0 instead of carrying out the expected calculation. Someone tell me what I’m doing wrong. Thank you.
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Enter odd number: ");
int n = Integer.valueOf(scan.nextLine());
scan.close();
double y = 0;
for (int x = 1; x < n + 1; x = x + 2) {
y += (x/(x + 1));
}
System.out.println("Y = " + y);
}
>Solution :
Problem is with the (x/(x + 1)). Variable x is of data type integer. This will also result in an integer when dividing. Integers are whole numbers and are simply cut to 0 at 0.x for this reason.
To fix this have a look here