Question: Why can't I get a decimal number in C++? So there's a question in my textbook. number = (1/3) * 3; cout < < (1/3)

Why can't I get a decimal number in C++?

So there's a question in my textbook.

number = (1/3) * 3;

cout << "(1/3) * 3 is equal to " << number;

the answer should be 0.99999...

While in the C++,

when I declared the variable number as integer. The output answer is 0. I got it because it won't calculate the decimal unless I told the program.

So I declared it again as double. The output is 1.

Then, I changed 1/3 to 1.0/3. (my textbook said if one number is int and another number is double, the output will be double )

However, my output is still 1.

Then, I declared it as double and changed the whole as (1.0/3.0)*3.0

The answer is still 1.

How can I get the 0.9999999999999... or 0.9(at least it approximate than 1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!