Write a program in Java to find the sum of the given series:
Categories: Core Java Java 8(JDK1.8) Experienced Freshers Java NCERT Series Programming
package r4rin.com.Series;
import java.util.stream.Stream;
/* Write a program in Java to find the sum of the given series:
S = a2 + a2 / 2 + a2 / 3 + …… + a2 / 10
*
*/
public class FindTheSumOfTheSeries {
public static void main(String[] args) {
int a = 8;//Change it
//JDK 1.7 or below versions
double sum = 0.0;
System.out.println("Series = ");
for (int i = 1; i <= 10; i++) {
System.out.print( Math.pow(a, 2) / i+" ");
sum += Math.pow(a, 2) / i;
}
System.out.println();
System.out.println("Sum = " + sum);
System.out.println();
// //JDK 1.8 or after versions
System.out.println("Series = ");
Stream.iterate(1, n->n+1).limit(10).map(i->Math.pow(a, 2)/i).forEach(i->System.err.print(i+" "));
double sumStream=Stream.iterate(1, n->n+1).limit(10).mapToDouble(i->Math.pow(a, 2)/i).sum();
System.out.println();
System.err.println("Sum = "+sumStream);
}
}