← Back

19. Remove Trailing Zeros

Medium

📝 Problem

Get a floating point value and print the floating point value without any trailing zeros.

Input Format:
Get a floating point value as input
Output Format:
Display the floating point value without trailing zeros
Constraints:
2.3E-308 to 1.7E+308
🎯 Why Mix C and C++? Smart Solutions with Limited Tools!

The Real-World Lesson

This problem teaches an important programming principle: when your current toolset is limited, sometimes borrowing from related technologies is the smartest solution!

💡 Why This Problem is Special
With basic C++ (cout and iomanip), removing trailing zeros is surprisingly difficult. But C's printf has a built-in format specifier (%g) that does this automatically! This is a perfect example of pragmatic programming.

1️⃣ Understanding the %g Format Specifier

The %g specifier is magical for this task:

  • ✅ Automatically removes trailing zeros
  • ✅ Removes trailing decimal point if no fractional part
  • ✅ Uses shortest representation (decimal or scientific)
  • ✅ Smart formatting based on the number's magnitude

2️⃣ How %g Works - Examples

Input With %f With %g
23.0900 23.090000 23.09
12.5200 12.520000 12.52
100.0 100.000000 100
0.00056 0.000560 0.00056

3️⃣ C vs C++ I/O - Quick Comparison

Feature C Style C++ Style
Input scanf("%lf", &num) cin >> num
Output printf("%g", num) cout << num
Header <cstdio> <iostream>
Type Safety ❌ No (manual format) ✅ Yes (automatic)
Trailing Zeros ✅ Easy with %g ❌ Complex

4️⃣ Understanding scanf and printf

scanf Syntax:
double num;
scanf("%lf", &num);  // %lf = long float (double)
                               // & = address-of operator
printf Format Specifiers:
// %f  - Fixed decimal (6 decimals default)
// %e  - Scientific notation
// %g  - Shortest representation (removes trailing zeros!)
// %lf - For double in scanf (not printf)

5️⃣ Why scanf Uses & (Address-Of)

scanf needs the memory address to store the value:

double num;
scanf("%lf", &num);  // & gives memory location
                    // scanf writes directly to that location

// Without & it would pass the value (wrong!)
// With & it passes the address (correct!)

6️⃣ Is Mixing C and C++ Bad Practice?

Short answer: No, but use wisely!
  • ✅ C++ is backward-compatible with C by design
  • ✅ Sometimes C functions are simpler for specific tasks
  • ✅ Many professional codebases mix both
  • ⚠️ Don't mix scanf/printf with cin/cout in same program (buffering issues)
  • ⚠️ Prefer C++ style when possible for type safety

7️⃣ Alternative: Pure C++ Solution (Advanced)

For reference, here's the complex C++ way (you don't need this for basic problems!):

// Complex C++ approach (avoid for beginners)
double num;
cin >> num;
string str = to_string(num);
// Remove trailing zeros manually...
// Much more code needed!

✅ Key Takeaways

  • %g automatically removes trailing zeros - perfect for this task!
  • C and C++ can work together - use the best tool for the job
  • scanf needs & to get the variable's address
  • printf offers powerful format control
  • Smart programmers know when to borrow from other toolsets
🧠 Programming Wisdom
"When your knowledge base is limited to basic tools, don't be afraid to use related technologies that make your life easier. That's how real-world programming works!"

Examples

Sample Input 1:
Input:
23.0900
Sample Output 1:
Output:
23.09
Trailing zeros removed: 23.0900 → 23.09
Sample Input 2:
Input:
12.5200
Sample Output 2:
Output:
12.52
Trailing zeros removed: 12.5200 → 12.52
More Examples:
100.0 → 100 (decimal point removed too!)
0.00560 → 0.0056 (trailing zero removed)
3.14159 → 3.14159 (no trailing zeros to remove)
Solution.cpp
#include <cstdio>

int main() {
    double num;
    
    // scanf: C-style input
    // %lf = long float (double)
    // &num = address where to store the value
    scanf("%lf", &num);
    
    // printf with %g: Smart formatting!
    // - Removes trailing zeros automatically
    // - Removes trailing decimal if no fraction
    // - Uses shortest representation
    printf("%g\n", num);
    
    return 0;
}