File tree Expand file tree Collapse file tree 2 files changed +11
-5
lines changed
Exercise_05/Exercise_05_23 Expand file tree Collapse file tree 2 files changed +11
-5
lines changed Original file line number Diff line number Diff line change 1515public class Exercise_05_23 {
1616 public static void main (String [] args ) {
1717 // Compute series from left to right
18- double sum = 0.0 ;
18+ double sumLeftToRight = 0.0 ;
1919 for (double i = 1.0 ; i <= 50000.0 ; i ++) {
20- sum += 1 / i ;
20+ sumLeftToRight += 1 / i ;
2121 }
2222
2323 // Display result of series sum from left to right
24- System .out .println ("Summation of series left to right: " + sum );
24+ System .out .println ("Summation of series left to right: " + sumLeftToRight );
2525
2626 // Compute series from right to left
27+ double sumRightToLeft = 0.0 ;
2728 for (double i = 50000.0 ; i >= 1.0 ; i --) {
28- sum += 1 / i ;
29+ sumRightToLeft += 1 / i ;
2930 }
3031
3132 // Display result of series sum from right to left
32- System .out .println ("Summation of series right to left: " + sum );
33+ System .out .println ("Summation of series right to left: " + sumRightToLeft );
34+
35+ // Compare the results
36+ System .out .println ("Summation of the series right to left - "
37+ + "Summation of the series left to right: "
38+ + (sumRightToLeft - sumLeftToRight ));
3339 }
3440}
You can’t perform that action at this time.
0 commit comments