C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Find Output of Program (Q.No. 5)
5.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    float a=3.15529;
    printf("%2.1f\n", a);
    return 0;
}
3.00
3.15
3.2
3
Answer: Option
Explanation:

float a=3.15529; The variable a is declared as an float data type and initialized to value 3.15529;

printf("%2.1f\n", a); The precision specifier tells .1f tells the printf function to place only one number after the .(dot).

Hence the output is 3.2

Discussion:
15 comments Page 1 of 2.

Palak said:   4 years ago
Wht if i dont want the decmal point to change from for eg : 3.09 to 3.1 when i am printing only one decimal point.

Pradeep Singh said:   5 years ago
@All.

1. In %2.1f, 2 is the left padding (blank spaces).
2. It is printing 3.2 instead of 3.1, because it is rounding of the float value.

Suraj kumar said:   6 years ago
@All.

Why the compiler is printing 3.2? instead it should have to print 3.1.

Because it should print one value after decimal that's why it is printing 2. Am I right?
(1)

Raeesa said:   7 years ago
What will be the output of printf("%f",(float)9.5)?

Nishtha Khare said:   7 years ago
Main()
{
float a=0.7;
if(a<0.7)
printf("c");
else
printf("c++");
}

Why the output of this program will be "c"?

Samarth Goyal said:   7 years ago
int main()
{
float a=3.15529;
printf("%b.1f\n", a);
return 0;
}

Varying b simply indents the result. What really matters or rather what will affect the output is the digit after the decimal.

say b=3, in that case the output will be printed upto 3 decimal places.

Anitha said:   8 years ago
int main()
{
float a=3.15529;
printf("%2.1f\n", a);
return 0;
}
2 here represents the 2positions to consider so we take 3.155, since it is 5 we round it as 3.2 and print the value is it.

Am I right?

Someone explain it if I'm wrong.

Mahmoud Abulaaty said:   8 years ago
@Chandu.

1244.567749 is the answer.

This is because you have already put a value into "a" and the max size of float is to give 1244.567749 not 1244.5678 as I think.

Chandu said:   9 years ago
What is the solution for this?

#include<stdio.h>
int main()
{
float a=1244.5678;
scanf("%3.2f",&a);
printf("%f",a);
}

Please explain it clearly.

Shirshendu Deb Roy said:   1 decade ago
printf("%9.2f",12.45);

This statement outputs four spaces and then 12.45. Those four spaces plus 12.45 (five characters total) equal the 9 in the width.

Only two values are shown to the right of the decimal because .2 is used in the %f conversion character.

#include <stdio.h>
int main()
{
float sample1 = 34.5;
float sample2 = 12.3456789;
printf("%%9.1f = %9.1f\n",sample1);
printf("%%8.1f = %8.1f\n",sample1);
printf("%%7.1f = %7.1f\n",sample1);
printf("%%6.1f = %6.1f\n",sample1);
printf("%%5.1f = %5.1f\n",sample1);
printf("%%4.1f = %4.1f\n",sample1);
printf("%%3.1f = %3.1f\n",sample1);
printf("%%2.1f = %2.1f\n",sample1);
printf("%%1.1f = %1.1f\n",sample1);
printf("%%9.1f = %9.1f\n",sample2);
printf("%%9.2f = %9.2f\n",sample2);
printf("%%9.3f = %9.3f\n",sample2);
printf("%%9.4f = %9.4f\n",sample2);
printf("%%9.5f = %9.5f\n",sample2);
printf("%%9.6f = %9.6f\n",sample2);
printf("%%9.7f = %9.7f\n",sample2);
printf("%%9.6f = %9.6f\n",sample2);
printf("%%9.7f = %9.7f\n",sample2);
printf("%%9.8f = %9.8f\n",sample2);
return(0);
}

Output:

%9.1f = 34.5
%8.1f = 34.5
%7.1f = 34.5
%6.1f = 34.5
%5.1f = 34.5
%4.1f = 34.5
%3.1f = 34.5
%2.1f = 34.5
%1.1f = 34.5
%9.1f = 12.3
%9.2f = 12.35
%9.3f = 12.346
%9.4f = 12.3457
%9.5f = 12.34568
%9.6f = 12.345679
%9.7f = 12.3456793
%9.8f = 12.34567928


Post your comments here:

Your comments will be displayed after verification.