Menu
Coddy logo textTech

Formatowanie wyników

Część sekcji Podstawy ścieżki C++ w Coddy. Lekcja 41 z 74.

challenge icon

Wyzwanie

Początkujący

Zmodyfikuj wynik tak, aby zawsze wyświetlał wartość typu double z dwoma miejscami po przecinku. Aby to zrobić, użyj operatora << wraz z std::fixed i std::setprecision(2):

#include <iomanip>

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

Trzeba dołączyć <iomanip>

Spróbuj swoich sił

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

Wszystkie lekcje w sekcji Podstawy

Poćwicz samodzielnie: Kompilator C++ online