write a program in java to input 50 numbers and display their sum
Share
write a program in java to input 50 numbers and display their sum
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
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