Conditional statement - 2#8521

Use a conditional statement to solve the problem. Since x \le 10000 = 10^4, then x^3 \le 10^{12}. Let's use the long long type to avoid overflow.

Algorithm implementation

Read the input value of .

scanf("%lld",&x);

Compute the value of .

if (x >= 10) 
  y = x * x * x + 5 * x;
else 
  y = x * x – 2 * x + 4;

Print the result.

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

Algorithm implementation — ternary operator

Read the input value of .

scanf("%lld",&x);

Compute the value of .

y = (x >= 10) ? x * x * x + 5 * x : x * x – 2 * x + 4;

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);
    long y, x = con.nextLong();
    if (x >= 10)
      y = x*x*x + 5*x;
    else
      y = x*x - 2*x + 4;
    System.out.println(y);
    con.close();
  }
}

Python implementation

Read the input value of .

x = int(input())

Compute the value of .

if x >= 10:
  y = x * x * x + 5 * x
else:
  y = x * x - 2 * x + 4

Print the result.

print(y)