8526. Conditional statement - 3

 

Find the value of y according to the following condition:

Input. One integer x (-100 ≤ x ≤ 100).

 

Output. Print the value of y according to the given condition.

 

Sample input 1

Sample output 1

3

0

 

 

Sample input 2

Sample output 2

-8

-3

 

 

SOLUTION

conditional statement

 

Algorithm analysis

Use a conditional statement to solve the problem.

 

Algorithm implementation

Read the input value of x.

 

scanf("%d", &x);

 

Compute the value of y.

 

if (x < -4)

  y = x + 5;

else

if (x <= 7)

  y = x * x – 3 * x;

else

  y = x * x * x + 2 * x;

 

Print the result.

 

printf("%lld\n",y);

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int y, x = con.nextInt();

    if (x < -4)

      y = x + 5;

    else

    if (x <= 7)

      y = x*x - 3*x;

    else

      y = x*x*x + 2*x;

    System.out.println(y);

    con.close();

  }

}  

 

Python implementation

Read the input value of x.

 

x = int(input())

 

Compute the value of y.

 

if x < -4:

  y = x + 5

elif x <= 7:

  y = x * x - 3 * x

else:

  y = x * x * x + 2 * x

 

Print the result.

 

print(y)