Como mostrar quantas possibilidades a msm variável pode ter?

1 resposta
javaprogramação
W
Bom dia!

Estou com uma questão onde tenho que mostrar números divisíveis por 3 e 6 ao mesmo tempo e exibir quantos são, consegui chegar ate aqui

Scanner in = new Scanner(System.in);

int x, y;

x = in.nextInt();

do{

y = in.nextInt();

}while (y < x);

for(int i = x; i <= y; i++){

if (i % 3 == 0 & i % 6 == 0){

System.out.println(i);}

}

Porém não estou conseguindo mostrar quantos são, alguém consegue me ajudar?

1 Resposta

staroski
import java.util.Scanner;

public class Exemplo {

    private static final Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("De:  ");
        int de = Integer.parseInt(in.nextLine());
        System.out.print("Até: ");
        int ate = Integer.parseInt(in.nextLine());
        int total = 0;
        System.out.printf("Números entre %d e %d divisíveis por 3 e 6:%n", de, ate);
        for (int numero = de; numero <= ate; numero++) {
            if (numero % 3 == 0 && numero % 6 == 0) {
                total++;
                System.out.printf("    %d%n", numero);
            }
        }
        System.out.printf("Total de números: %d%n", total);
    }
}

Exemplo de saída:

De:  11
Até: 37
Números entre 11 e 37 divisíveis por 3 e 6:
    12
    18
    24
    30
    36
Total de números: 5
Criado 13 de novembro de 2020
Ultima resposta 13 de nov. de 2020
Respostas 1
Participantes 2