[Solved] Age computation always off by one


Let father’s current age = f

son’s current age = s

let x years back, father was twice as old as son.

then 2(s-x) = (f-x)
=> x = 2*s – f

Note: if x comes negative, then after x years , father will be twice as old as son(test for input[25,5])

public static void main(String[] args) {
    int x = twiceAsOld(25, 5);
    System.out.println(x);
  }

  public static int twiceAsOld(int dadYears, int sonYears) {
    return 2*sonYears - dadYears;

  }

2

solved Age computation always off by one