003-006-007

Overflow and Type Casting

Hard

Problem Description

Overflow and Type Casting

In this problem, you will create a program that reads a long value from standard input, demonstrates overflow when casting it to int, implements safe conversion logic using a conditional check, and displays the results to standard output.

Learning Objective: Understand overflow during type conversion and how to handle it

Overview

Create a program that reads a long value from standard input, demonstrates overflow when casting it to a smaller type, and implements safe conversion.

Specifications

  • Read a long value from standard input and assign it to bigValue
  • cast directly to int and assign to overflowValue
  • Use conditional logic to safely cast and assign to safeValue (use Integer.MAX_VALUE if overflow, Integer.MIN_VALUE if underflow)
  • Output all three values

Input Format

a long integer value

Output Format

Original: <bigValue>
Overflow: <overflowValue>
Safe: <safeValue>

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up