Accept two integers and print quotient with 16 decimal places and print the remainder in next line.
The Challenge: Integer Division Problem
When dividing two integers in C++, you get integer division by default:
54 / 65 = 0
(not 0.83!). To get the decimal quotient, we need to convert
one operand to a floating-point type. But how we do this matters!
static_cast
.
While old C-style casting like (double)a
works, static_cast<long double>(a)
is the preferred, safer, and more professional way in C++!
The Problem:
long long a = 54, b = 65;
// β Integer division (wrong!)
long double quotient = a / b;
// Result: 0.000000... (because 54/65 = 0 in integer math)
// β
Correct: Cast to floating-point first
long double quotient = static_cast<long double>(a) / b;
// Result: 0.8307692307692308 (correct!)
Aspect | C-Style Cast | static_cast |
---|---|---|
Syntax | (double)a |
static_cast<double>(a) |
Type Safety | β Weak - allows dangerous casts | β Strong - compile-time checking |
Readability | β οΈ Hard to spot in code | β Clear and explicit |
Searchability | β Hard to find with grep/search | β Easy to search for "static_cast" |
C++ Style | β οΈ Legacy (C compatibility) | β Modern C++ standard |
// Method 1: C-Style Cast (old way)
quotient = (long double)a / b; // Works, but not recommended
// Method 2: static_cast (modern C++ way) β
quotient = static_cast<long double>(a) / b;
// Why static_cast is better:
// 1. Compiler catches invalid conversions at compile-time
// 2. More visible in code reviews
// 3. Self-documenting - shows intent clearly
// 4. Industry standard in professional C++ code
long double quotient = a / b;
performs integer division FIRST,
then converts the result (0) to long double. Always cast BEFORE division!
Modern C++ provides four specialized casting operators:
Cast Type | Purpose | When to Use |
---|---|---|
static_cast |
Type conversion | Converting between related types (intβdouble) |
dynamic_cast |
Runtime polymorphism | Safely casting in inheritance hierarchies |
const_cast |
Remove const/volatile | Modifying const-ness (use sparingly!) |
reinterpret_cast |
Low-level reinterpretation | Pointer/binary manipulation (advanced) |
static_cast
is perfect because
we're doing a simple, safe type conversion from integer to floating-point.
long long a = 54, b = 65;
// 1. Floating-point division (/) - Gets quotient
long double quotient = static_cast<long double>(a) / b;
// quotient = 0.8307692307692308
// 2. Modulo operator (%) - Gets remainder
long long remain = a % b;
// remain = 54 (since 54 < 65, remainder is 54)
// Mathematical relationship:
// a = (b Γ integer_quotient) + remainder
// 54 = (65 Γ 0) + 54 β
Using setprecision(16)
with long double
ensures maximum accuracy
for the quotient. The long double
type provides approximately 18-19 significant
digits, and 16 decimal places give us high precision without rounding errors.
static_cast
is essential for professional C++ programming!
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long long a, b, remain;
long double quotient;
cin >> a >> b;
quotient = static_cast<long double>(a) / b;
remain = a % b;
cout << fixed << setprecision(16) << "The quotient when " << a << " is divided by " << b << " is " << quotient << endl;
cout << "The remainder when " << a << " is divided by " << b << " is " << remain;
return 0;
}