Output floating-point data in fixed point notation. - C++ Data Type

C++ examples for Data Type:float

Description

Output floating-point data in fixed point notation.

Demo Code

#include <iostream>
#include <iomanip>
#include <cmath>                       // For square root function
using std::cout;//from   w w w .j  a va 2 s  .co  m
using std::cin;
using std::sqrt;

int main()
{
  const double fish_factor {2.0/0.5};  // Area per unit length of fish
  const double inches_per_foot {12.0};
  const double pi {3.14159265};

  double fish_count {};                // Number of fish
  double fish_length {};               // Average length of fish

  cout << "Enter the number of fish you want to keep: ";

  cin >> fish_count;

  cout << "Enter the average fish length in inches: ";

  cin >> fish_length;
  fish_length /=inches_per_foot;      // Convert to feet

  double pond_area {fish_count * fish_length * fish_factor};

  double pond_diameter {2.0 * sqrt(pond_area/pi)};

  cout << "\nPond diameter required for " << fish_count << " fish is "
     << std::fixed << std::setprecision(2)
     << pond_diameter << " feet.\n";                // Output value is 8.74
}

Result


Related Tutorials