Menu
CoddyTech

Đầu ra có định dạng

Thuộc phần Kiến thức nền tảng trong hành trình C++ của Coddy. Bài học 41 trên 74.

challenge icon

Thử thách

Người mới

Hãy chỉnh sửa đầu ra để luôn in giá trị số thực với hai chữ số thập phân. Để làm vậy, hãy dùng toán tử << cùng với std::fixed và std::setprecision(2):

#include <iomanip>

std::cout << std::fixed << std::setprecision(2) << num;

Bạn cần thêm <iomanip>

Tự làm thử

#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;
}

Tất cả bài học trong Kiến thức nền tảng

Tự luyện tập: Trình biên dịch C++ online