Thursday, 11 February 2016

Suppose x1 and x2 are two type double variables

Leave a Comment
Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so.What if you want to add them as type double and then convert to int?

Ans:

Either of the following would work for the first task:

int pos = (int) x1 + (int) x2;
int pos = int(x1) + int(x2);

To  a d d  t h e m a s  t y p e  double and then convert, you could do either of the following:

int pos = (int) (x1 + x2);
int pos = int(x1 + x2);


Reference:

Answers to Chapter Reviews C++ Primer Plus

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: