Formatted Output
Part of the Fundamentals section of Coddy's C++ journey — lesson 41 of 74.
Challenge
BeginnerModify the output so that it will always print a double value with two decimal places. To do it use the << operator with std::fixed and std::setprecision(2):
#include <iomanip>
std::cout << std::fixed << std::setprecision(2) << num;You'll need to include <iomanip>
Try it yourself
#include <iostream>
int main() {
std::cout << "Calculator App" << std::endl;
double num1;
std::cin >> num1;
double num2;
std::cin >> num2;
// Perform arithmetic operations
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
double division = num1 / num2;
// Print the results
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Division: " << division << std::endl;
return 0;
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else