Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FractionSeries {
- public static void main(String[] args) {
- //sum of fractions
- double sum = 0.0;
- //denominator
- int d;
- //loop from 1 to 19 in steps of 2
- for (int n=1; n<20; n+=2) {
- // calc denominator
- d=n+1;
- //display current fraction
- System.out.printf("%d/%d", n, d);
- //if not final fraction, print +
- if (n<19) System.out.print("+");
- //otherwise print =
- else System.out.print(" = ");
- //add floating point result of (n divided by d) to sum
- sum += (n*1.0)/d;
- }//for
- //print result
- System.out.println(sum);
- }//main()
- }//class FractionSeries
Advertisement
Add Comment
Please, Sign In to add comment