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 15
15
public class Exercise_05_23 {
16
16
public static void main (String [] args ) {
17
17
// Compute series from left to right
18
- double sum = 0.0 ;
18
+ double sumLeftToRight = 0.0 ;
19
19
for (double i = 1.0 ; i <= 50000.0 ; i ++) {
20
- sum += 1 / i ;
20
+ sumLeftToRight += 1 / i ;
21
21
}
22
22
23
23
// 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 );
25
25
26
26
// Compute series from right to left
27
+ double sumRightToLeft = 0.0 ;
27
28
for (double i = 50000.0 ; i >= 1.0 ; i --) {
28
- sum += 1 / i ;
29
+ sumRightToLeft += 1 / i ;
29
30
}
30
31
31
32
// 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 ));
33
39
}
34
40
}
You can’t perform that action at this time.
0 commit comments