KnowledgeBoat Logo

Computer Applications

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}

Java

Java Conditional Stmts

2 Likes

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 Write a program that will read the value of x and compute the following function:

Answered By

1 Like


Related Questions