0
Write a java series program: 5, 10, 15, 20, 25, 30, 35âŠâŠâŠâŠâŠâŠâŠâŠâŠâŠup to n terms.Â
5 Answers
+ 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);
}
}
+ 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));
}
}
+ 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.
+ 2
0_O-[MĂ€gĂĄr_SĂĄm_ĂkĂ _NĂŒllpÞïntĂȘr_ĂxĂ«cĂ©ptïön]~~ ohh thankyou for explanation
0
0_O-[MĂ€gĂĄr_SĂĄm_ĂkĂ _NĂŒllpÞïntĂȘr_ĂxĂ«cĂ©ptïön]~~ can you explain how your forEach woRkiNg