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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started