0

Write a java series program: 5, 10, 15, 20, 25, 30, 35…………………………up to n terms. 

11th Nov 2020, 8:44 AM
Santhosh Reddy
Santhosh Reddy - avatar
5 odpowiedzi
+ 5
For divisible case only, import java.util.stream.IntStream; public class Program { public static void main(String[] args) { int n=100; IntStream.rangeClosed(1,n). filter(i -> i % 5 == 0). forEach(System.out::println); } }
11th Nov 2020, 9:30 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 5
♨️♨️ forEach() method is to iterate the elements(Iterable and Stream), takes single parameter interface function, using method references (which just refers to println() method). in simple demonstration, import java.util.stream.IntStream; interface Printable{ void printInt(int param); } public class Program { public static void main(String[] args) { //Referencing println() method Printable print=System.out::println; int n=100; IntStream.rangeClosed(1,n). filter(i -> i % 5 == 0). forEach(i -> print.printInt(i)); } }
11th Nov 2020, 11:13 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
Take input n from user. Initialize a variable num = 5. Run a for loop which iterates for i<n. Inside that print num. Then increment num by 5.
11th Nov 2020, 9:07 AM
Avinesh
Avinesh - avatar
11th Nov 2020, 3:10 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ can you explain how your forEach woRkiNg
11th Nov 2020, 10:07 AM
A S Raghuvanshi
A S Raghuvanshi - avatar