brilliant_moves

FractionSeries.java

Apr 28th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.61 KB | None | 0 0
  1. public class FractionSeries {
  2.     public static void main(String[] args) {
  3.         //sum of fractions
  4.         double sum = 0.0;
  5.         //denominator
  6.         int d;
  7.         //loop from 1 to 19 in steps of 2
  8.         for (int n=1; n<20; n+=2) {
  9.             // calc denominator
  10.             d=n+1;
  11.             //display current fraction
  12.             System.out.printf("%d/%d", n, d);
  13.             //if not final fraction, print +
  14.             if (n<19) System.out.print("+");
  15.             //otherwise print =
  16.             else System.out.print(" = ");
  17.             //add floating point result of (n divided by d) to sum
  18.             sum += (n*1.0)/d;
  19.         }//for
  20.         //print result
  21.         System.out.println(sum);
  22.     }//main()
  23. }//class FractionSeries
Advertisement
Add Comment
Please, Sign In to add comment