samedi 19 décembre 2020

If condition different answer

The question is to print the outputs for the following conditions

  • odd -> Weird

  • even and between 2-5 -> Not weird 1

  • even and between 6-20 -> Weird 2

  • even and greater than 20 -> Not Weird

    n =N%2;
    if (n != 0){
        System.out.println("Weird");
    }
    else if(n ==0 &&  n<=5){
        System.out.println("Not Weird 1");
    }
    else if(n==0 && n>=6  && n<20){
        System.out.println("Weird 2");
    }
    
    else if (n==0 && n>=20){
        System.out.println("Not Weird");
        }
        else{
            System.out.println("");
        }
    }
    

So I'm getting Not Weird 1 as the output when I enter 8. It should be Weird 2 which is being displayed. Where did I go wrong? I can't find anything wrong in it

Aucun commentaire:

Enregistrer un commentaire