Java is the implementation code that calculates the sum of the first 20 items of a fractional sequence

  • 2020-06-15 08:33:07
  • OfStack

Title: 1 fraction series: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13... Find the sum of the first 20 terms of this sequence.

Program analysis: please grasp the change of the numerator and denominator.

Program design:


public class test20 {
  public static void main(String[] args) {
    float fm = 1f;
    float fz = 1f;
    float temp;
    float sum = 0f;
    for (int i=0;i<20;i++){
       temp = fm;
       fm = fz;
       fz = fz + temp;
       sum += fz/fm;
       //System.out.println(sum);
    }
    System.out.println(sum);
  }
}


Related articles: