P
P
Piotr2015-04-20 23:42:17
Java
Piotr, 2015-04-20 23:42:17

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

2 answer(s)
P
poisonGreen, 2015-04-21
@Nebaddonn

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);
    }

------------------
for java 8 :
public static void main(String[] args) {
        int sum = IntStream.range(1, 100).filter(i -> (i%9!=0)).sum();
        System.out.println("sum = " + sum);
}

I
IceJOKER, 2015-04-20
@IceJOKER

Kind.
if(i % 9 != 0) //if the remainder of division by 9 is not equal to zero
//then start summing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question