Question

Why can't I get a decimal number in C++? So there's a question in my textbook....

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)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It is because (1/3)*3 is equal to 1. You can even try it on the calculator.

(1/3)*3 =3 which is recurring decimal or non-terminating decimal number 0.9999...

If you want to specify the number of decimal digits in the output, you can do it by the function setprecision(int n) where n is the number of digits you want. You will also need to declare an additional header file - #include<iomanip>.

Example-

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
float number = (1.0/3);
cout<<setprecision(2)<<number<<endl;
cout<<setprecision(3)<<number;

return 0;
}

The output would be-

0.33
0.333
Add a comment
Know the answer?
Add Answer to:
Why can't I get a decimal number in C++? So there's a question in my textbook....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. I can get to work by either riding my bicycle riding my motorcycle, or driving...

    2. I can get to work by either riding my bicycle riding my motorcycle, or driving my car. I'm notoriously bad about maintaining my 3 transportation options. From previous experience, I know there's a 30% likelihood my bicycle will have a flat tire, a 37% likelihood my motorcycle battery will be dead (so it won't start), and a 81% chance that my car will run. I'm running late and I don't have time to fix any of these possible problems...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • Why am I getting the same results with my rand() every time I run my C++...

    Why am I getting the same results with my rand() every time I run my C++ program? I am trying to do this problem: each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $ 5000, or a total of $650. Write a program(using an array of counters) that determines how many of the salespeople earned salaries...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • Having some coding issues. I can't seem to figure out why my calculation is coming out...

    Having some coding issues. I can't seem to figure out why my calculation is coming out incorrect for the fourth column. For example, mathematically speaking the first row, columns 1-3: ​19, 68, 10. Using the specific equation: finalGrade = e1*.30 + e2*.30 + f*.40. I should get 30.1 in the 4th column but I get otherwise. See provided screenshot. Please explain what I am doing wrong? /* Program info: Fill dd array 30 and 3 of grades Double course[30][3](rows =...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • c++ I am suppose to write a function that gets a number from a user, say...

    c++ I am suppose to write a function that gets a number from a user, say 3.123456789 and then the program gets another number from the user which is used to find out how many decimal digits we want rounded to. So if the user inputs 3 then the number would turn into 3.123 then we are suppose to add two digits next to the rounded number so it would turn into 3.12300. the rounding is suppose to be done...

  • I can't get my code to work on xcode and give me an output. #include <conio.h> #include <cstdlib> #incl...

    I can't get my code to work on xcode and give me an output. #include <conio.h> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; // So "std::cout" may be abbreviated to "cout" //Declare global arrays int dummy1[10]; int dummy2[10]; int dummy3[10]; int universalSet[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Function to return statement "Empty thread" when the resultant set is empty string isEmpty(int arr[]) { string...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • Help! i can't get the correct output for this question: Write a program that will read...

    Help! i can't get the correct output for this question: Write a program that will read in an integer from the user and test to see whether or not it is prime. You are to determine if each integer read is prime or not, and output the result to the screen as shown below. If a number is prime, your output should read: 101 = 1 x 101 101: PRIME Number For a number that turns out not to be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT