Can someone please explain me the reasons for output....for this programme | SoloLearn: Learn to code for FREE!

+1

Can someone please explain me the reasons for output....for this programme

Code: #include <stdio.h> void main() { float g=123.456789; printf("\n %.1f",g); printf("\n %.2f",g); printf("\n %.3f",g); printf("\n %.4f",g); } Output: 123.5 123.46 123.457 123.4568 My doubts are in comment section

4 Answers

New Answer

+1

Why is in the output 4 missing in first line 5 missing in second line 6 missing in third line And 7 missing in fourth line...???

+1

For me the output is: 1231 1232 1233 1234 int main() { float g=123.456789; printf("%.f1 ",g); printf("%.f2 ",g); printf("%.f3 ",g); printf("%.f4 ",g);

+1

The fractal portion of the number is rounded up to fit in <x> digit allowed - as specified by format specifier "%.xf". "%.1f" (only 1 decimal point) 123.456789 => rounded up to 123.5 "%.2f" (2 decimal points) 123.456789 => rounded up to 123.46 "%.3f" (3 decimal points) 123.456789 => rounded up to 123.457 "%.4f" (4 decimal points) 123.456789 => rounded up to 123.4568 Hth, cmiiw