Even numbers, and number of terms to display

1

I have a question, make the following program that returns the even numbers that are within a limit that the user grants by keyboard, is it possible some way for me to deploy, the number of even terms instead of those that Are they considered within the limit? example: Enter limit:

5

The even numbers are:

2,4,6,8,10

Instead of:

2, 4

    Scanner  sc = new Scanner (System.in);
    int li=0;
    System.out.println("Ingrese la cantidad de numeros pares: ");
    li= sc.nextInt();
    for (int i =2; i<= li; i++)
    {
        if(i%2==0)
        {
            System.out.print(i+ ", ");
        }

    }
    System.out.println("");
    
asked by Axwell Duarte 20.10.2018 в 23:45
source

2 answers

1
  //Haz un bucle for que se repita tales veces dependiendo de cuántos números pares necesites

  Scanner  sc = new Scanner (System.in);
    int cant=0;
    System.out.println("Ingrese la cantidad de numeros pares: ");
        cant= sc.nextInt();
    for(int i=1;i<=cant;i++){
    System.out.println(2*i);// el valor de i va incrementando a medida que va pasando el bucle for (1, 2, 3...n)
}
/*    Por ejemplo, la salida para el valor 10 serían, 10 iteraciones en el bucle for:
    -1: 1*2=2
    -2: 2*2=4
    -3: 3*2=6
    -4: 4:2=8
    -5: 5*2=10
    -6: 6:2=12
    .
    .
    .
*/

//A petición también lo hago con números impares:
Scanner  sc = new Scanner (System.in);
        int cant=0;
        System.out.println("Ingrese la cantidad de numeros pares: ");
            cant= sc.nextInt();
        for(int i=0;i<cant;i++){ //comienzo el valor i desde cero
        System.out.println(2*i+1); //sumo un valor a la salida
    
answered by 21.10.2018 в 00:13
1

Here is a solution to do what you need and be able to choose what we want to get, even or odd. I hope it serves you, I have commented so you understand what I do in each line.

Code:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Ingrese la cantidad de numeros: ");
        int cantidad = scanner.nextInt();

        System.out.print("Desea par o impar? (1 = par, 2 = impar): ");
        int tipo = scanner.nextInt();

        boolean isPar = tipo == 1 ? true : false;

        // Aquí guardaremos los números encontrados
        ArrayList<Integer> numeros = new ArrayList<>();

        for (int i = 0; i < 999; i++) {

            // Si ya tenemos x números, salir del for
            if (numeros.size() == cantidad)
                break;

            if (isPar) {
                // Par
                if (i % 2 == 0) {
                    numeros.add(i);
                }
            } else {
                // Impar
                if (i % 2 != 0) {
                    numeros.add(i);
                }
            }
        }

        // Imprimir
        for (Integer numero : numeros)
            System.out.println(Integer.toString(numero));
    }
}

Result:

  

Enter the number of numbers: 5

     

Do you want odd or even? (1 = pair, 2 = odd): 1

     

0 2 4 6 8

    
answered by 21.10.2018 в 00:48