← Back

23. Division - Quotient and Remainder

Medium

πŸ“ Problem

Accept two integers and print quotient with 16 decimal places and print the remainder in next line.

Input Format:
Accept two integers as input
Output Format:
Print Quotient and Remainder as follows:
The quotient when NUM1 is divided by NUM2 is QUOTIENT_VAL
The remainder when NUM1 is divided by NUM2 is REM_VAL
Constraints:
1 ≀ N1, N2 ≀ 10^15
Sample Input 1:
54 65
Sample Output 1:
The quotient when 54 is divided by 65 is 0.8307692307692308
The remainder when 54 is divided by 65 is 54
Sample Input 2:
23 78
Sample Output 2:
The quotient when 23 is divided by 78 is 0.2948717948717949
The remainder when 23 is divided by 78 is 23
🎯 Mastering Type Casting: static_cast vs C-Style Casting

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!

πŸ’‘ Why This Question is Special
This problem introduces modern C++ type casting with 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++!

1️⃣ Understanding Type Casting

The Problem:

πŸ“Œ Integer Division Issue:
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!)

2️⃣ C-Style Cast vs static_cast

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

3️⃣ Why static_cast is Better

πŸ“Œ Comparison Example:
// 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
⚠️ Common Mistake to Avoid
long double quotient = a / b; performs integer division FIRST, then converts the result (0) to long double. Always cast BEFORE division!

4️⃣ The Four C++ Cast Operators

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)
πŸ’‘ Pro Tip: For this problem, static_cast is perfect because we're doing a simple, safe type conversion from integer to floating-point.

5️⃣ Division Operations Explained

πŸ“Œ Two Types of Division:
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  βœ“

6️⃣ Why 16 Decimal Places?

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.

πŸš€ Real-World Application
Type casting is everywhere:
  • Financial calculations (currency conversion)
  • Scientific computations (unit conversions)
  • Graphics programming (coordinate transformations)
  • Data analysis (percentage calculations)
Mastering static_cast is essential for professional C++ programming!
πŸ’» Solution Code
#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;
}