Answer the question
In order to leave comments, you need to log in
Building a loop with an exception?
Goodnight.
There is a task - to sum up numbers from 1 to 100, excluding those that are divisible by 9 without a remainder. There is an idea that it is necessary to build in the form of two cycles - the first one checks for truth - whether the result of the division is int or double, the second one already performs the addition and displays the result. But how to organize - I can not figure it out.
Answer the question
In order to leave comments, you need to log in
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <=100; i++) {
if(i%9 != 0 ){
sum+=i;
}
}
System.out.println("sum = " + sum);
}
public static void main(String[] args) {
int sum = IntStream.range(1, 100).filter(i -> (i%9!=0)).sum();
System.out.println("sum = " + sum);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question