Write a program in Java to display the first 10 terms of the following series: 10, 20, 30, 40, ……..
Categories: Core Java Java 8(JDK1.8) Interview questions and answers Experienced Freshers Java Study Lambda NCERT Series Programming
package r4rin.com.Series;
/*
* Write a program in Java to display the first 10 terms of the following series:
10, 20, 30, 40, ……..
*
*/
import java.util.stream.Stream;
public class TenTermsOfSeries {
public static void main(String[] args) {
/*
* Using For Loop befor JDK1.8
* Write a program in Java to display the first 10 terms of the following series:
10, 20, 30, 40, ……..
*/
for(int i=10;i<=100;i+=10) {
System.out.print(i+",");
}
/*Using Stream API -JDK 1.8
* Write a program in Java to display the first 10 terms of the following series:
10, 20, 30, 40, ……..
*/
System.out.println();//New Line
////Stream.iterate(initial value, next value)
Stream.iterate(10, n->n+10).limit(10).forEach(x->System.err.println(x));
}
}
Output :-
10,20,30,40,50,60,70,80,90,100,
10
20
30
40
50
60
70
80
90
100
- Here we use two ways to generate series.
- You can use any one But the best is using Stream API It will work JDK 1.8 or after all versions of java.
- It will not work JDK 1.7 or before versions.