ICSE Class 10 Computer Applications Question 25 of 43

Conditional Constructs in Java — Question 25

Back to all questions
25
Question

Question 25

Write a program that will read the value of x and compute the following function:

y={7for x > 00for x=07for x < 0y = \begin{cases} 7 &\text{for } \text{x } \text{\textgreater} \text{ 0} \\ 0 &\text{for } x = 0 \\ -7 &\text{for } \text{x } \text{\textless} \text{ 0} \end{cases}

Answer
import java.util.Scanner;

public class KboatFunction
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter x: ");
        int x = in.nextInt();
        int y = -1;
        if (x > 0)
            y = 7;
        else if (x == 0)
            y = 0;
        else
            y = -7;
            
        System.out.println("y=" + y);
    }
}
Output
BlueJ output of KboatFunction.java