+ 1

Why the screen can only show 6 numbers even though I have used datatype "long double"?

I am trying to input the first term, the second term and the terms number of a geometric sequence to find the geometric series. #include <iostream> using namespace std; int main() { double a,b,n,s,p,r; cin >> a >>b>>n; r= b/a; p=1; for(int i=0;i<n;i++) { p=p*r; }; s=(a-a*p)/(1-r); cout<< s <<endl; return 0; }

29th Mar 2022, 2:26 AM
Anny
Anny - avatar
2 Answers
+ 4
I find that the printf function is a little nicer than cout for precision output. You could replace the cout statement with this: printf("%lf\n", s); There is a way to control precision in cout, but once you set it, you cannot reset cout's properties within the program back to the default automatic formatting. #include <iomanip> . . . cout << setprecision(42) << s <<endl;
29th Mar 2022, 4:16 AM
Brian
Brian - avatar
0
It helps. Thank you
30th Mar 2022, 12:48 AM
Anny
Anny - avatar