vendredi 25 octobre 2019

How can i simplify this odd numbers code?

Is there a way to simplify this code of a program that print the odd numbers between 2 numbers that the user inputs, idk, maybe in the declaration of the for. I mean, do the same, but without the if, only the for?

int n1,n2;
System.out.println("Enter first number");
n1 = keyboard.nextInt();
System.out.println("Enter second number");
n2 = keyboard.nextInt();
for (int i=n1; i < n2; i++)
{
    if( i % 2 != 0)
    {
        System.out.println(i + " ");
    }
}

Aucun commentaire:

Enregistrer un commentaire