Tuesday, October 22, 2019

Simple use of if - else

Question

Given an integer,n, perform the following conditional operations,

If n is odd, print Good

If n is even and in the inclusive range of 2 to 5, print Not Good

If n is even and in the inclusive range of 6 to 20, print Good

If n is even and greater than 20, print Not Good

Program

package ifelse;
import java.util.Scanner;
public class IfElse
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        if(N%2==1)
        {
            System.out.println("Good");
        }
        else
        {
            if(N >= 2 && N <= 5)
                System.out.println("Not Good");
            else if(N >= 6 && N <= 20)
                System.out.println("Good");
            else if(N > 20)
                System.out.println("Not Good");
        }
    }
}

Video  explanation




No comments:

Post a Comment