Do integral sum

closed account (EwCjE3v7)
Exercise:
Assuming i is an int and d is a double, write the expression i *= d so that it does integral, rather than floating-point multiplycation


This is what I got

1
2
3
4
5
    int i = 5;
    double d = 4.25;
    int sum = static_cast<int>(i) * d;

    cout << sum << endl;


But I think I`m wrong
Last edited on
i is already an int, there is no reason to cast it to an int. Both operands to the multiplication operator must be integral to perform integral multiplication. You're very close! By the way, you should name the result variable product and not sum.
Last edited on
closed account (EwCjE3v7)
Thank you LB, so there is no way to do a integral sum? Because what i *= d does is that it multiplies them and return 21.25.

Wouldn`t this work?

1
2
3
4
int i = 5;
    double d = 4.25;
    i *= static_cast<int>(d); // does 5 * 4
    cout << i << endl; // output is 20 
Last edited on
The assignment you quoted asks for integral multiplication, not integral sum.
Topic archived. No new replies allowed.