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;
}
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 2 of 2.
Nishtha Khare said:
8 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"?
{
float a=0.7;
if(a<0.7)
printf("c");
else
printf("c++");
}
Why the output of this program will be "c"?
Raeesa said:
8 years ago
What will be the output of printf("%f",(float)9.5)?
Suraj kumar said:
7 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?
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)
Pradeep Singh said:
6 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.
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.
(1)
Palak said:
5 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers