cpp output

Can anyone explain how to print output in CPP in the following way:
if the answer is 11, it should print 11.0, but if the answer is the square root of 3, it should print 1.732051 and so on…
I mean how to change the decimal point depending on the answer. I have tried setprecision but it doesn’t seem to help.

If you know how many decimal places you need to output you can add fixed after setprecision(x)

x = 6;
cout << setprecision(x) << fixed << sqrt(3) << endl;

Had i been there, I would have tried something on lines of-

Let root have the answer we have t print.

int q =root; //integral part of answer
if ( q*q == OriginalNumber)
    print upto 1 decimal using "printf ("%.1lf",root);"
else
    print upto 6 deciamls (or the number of decimals needed, acc to Q) (again, by "printf ("%.6lf", ans);"

From what I am able to grasp from the Q, if the ans is integer like 10, 11, 12 etc then we just have to add 1 decimal, and if its not an integral value, like 1.7324… then 6 decimals.

(Note- The above method is true if Q states that the number [whose square root we have to find] is strictly an integer.)

The Q is ambiguous/needs clarification for cases where the number given is of form 25.6 x 25.6 or 28.71 x 28.71 (meaning will the ans be of form 28.710 or 28.710000)

EDIT-If this is some Q you came across, can you provide the link?

@ayush933 Suppose you know upto how many decimal places you want the answer(ans) and let that be ‘t’
then write the answer as printf(".tf",ans) Eg. you know that you want answer upto 6 decimal places write it as printf(".6f",ans).Hope you got this. :slight_smile:

See my code below

#include<bits/stdc++.h>
using namespace std;

int main()
{
//Your number
float ans=11;
if( ans == 11)
{
    cout << setprecision(1) <<fixed << ans;
}
else
    {
        ans=sqrt(3.0);
        cout<< setprecision(6) << fixed << ans;
    }

}

Click on there to know more about ‘setprecision’ and ‘fixed’

I hope you will get this simpler and easy code.

I used 11 and square root of 3 just as an example.
There are numeral test cases, answers to which I don’t know.
How to do it there?

Yes if the ans is an integer then only one decimal point but if its not an integer then not exactly 6 decimal point.
In that case, the number of decimal points would depend on answer itself
like the answer would 28.710 and not 28.710000.

1 Like

Just change this code according to the requirement otherwise tell me the whole question in proper format so that i will provide you full and clear cut explanation. :slight_smile:

Okayy. I see. If the number can be a decimal, then we cannot use “q*q == OriginalNumber” since “==” operator should not be used to compare 2 floats (Errors come in way).

For 28.71 case, one important thing to know is that this means that the inputted number is not an integer. (a decimal number x decimal number = a decimal number.)

hmm…thinking on this I can only think of a naïve implementation atm. Can you quote the question? That would help.

//