For - loop loop

Asked

Viewed 114 times

-2

Hello, I am new here and in the world of programming. I would like to know how to do the last part of this exercise using For. So far I have done this:

for(int i = 1; i<=100; i++) {
    if(i%2==0) {
        System.out.println(i+" é par");
    }else {
        System.out.println(i+" é ímpar");
    }

    if(i%3==0){
        System.out.println("é múltiplo de 3");
    }

    if(i%4==0) {
        System.out.println("múltiplo de 4");
    }

inserir a descrição da imagem aqui

  • 2

    What is the question specifically ? Checking if the numbers are primes ? What have you already done for this purpose ? Where did you get stuck ? I don’t see any code in the question concerning that

  • Boy, I wanted to know first how to make that second go, inside the if, inside the other for.

  • My suggestion is to do each thing individually, and in the end put it all together, as if it were a piece. This way you learn how to do each part. In this sense the next step would be to make a code to verify if a number is prime. Then if you succeed, put the two parts together in the right way.

  • Right. Thank you very much ✌

1 answer

1


The second part of your problem:

if (i % 5 == 0){
    for(int a = 0; a < i; a++){
        if(a % 5 == 0){
            System.out.println("O número " + a + "é multiplo de 5 e anterior a " + i);

        }
    }
}

Regarding prime numbers, I did not understand which number should be compared.

  • 1

    The exercise wants to know if each number to be printed is prime or not.

  • 1

    Actually, it would be more efficient if you did for(int a = 0; a < i; a += 5).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.