write a program in java to input 50 numbers and display their sum
Answers
Answered by
4
public static void main(String[] commandLine) {
int total = IntStream.rangeClosed(10, 100)
.filter(i -> i % 2 == 0)
.sum();
System.out.println( String.format("The total is %d", total) );
} I think will work, if I’ve got it right.
IntStream.rangeClosed(10, 100) supplies all the integers between 10 and 100 inclusive as a stream
filter(i -> i % 2 == 0) creates a new stream, containing only the numbers where number mod 2 equals zero - even numbers
sum() handle adds up all the integers in this new stream and returns an int
Similar questions