0
Write a java series program: 5, 10, 15, 20, 25, 30, 35…………………………up to n terms.
5 Respostas
+ 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