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 1 of 2.
Sriram said:
1 decade ago
What is the purpose of 2 in "2. 1f"?
Atul sharma said:
1 decade ago
It mean there are all 2 decimal values which is going to print and. 1 mean there is one value after decimal.
Dinesh said:
1 decade ago
No it's wrong sharma. 2 means it takes 2 empty spaces before printing a value on the screen.
Ash said:
1 decade ago
int main()
{
float a=3.15529;
printf("%3.1f\n", a);
return 0;
}
What is the answer for this?
{
float a=3.15529;
printf("%3.1f\n", a);
return 0;
}
What is the answer for this?
Shekhar said:
1 decade ago
3.2 is the answer.
3 means it will take 3 decimal values into consideration.
3 means it will take 3 decimal values into consideration.
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
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
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.
#include<stdio.h>
int main()
{
float a=1244.5678;
scanf("%3.2f",&a);
printf("%f",a);
}
Please explain it clearly.
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.
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.
Anitha said:
7 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.
{
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.
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.
{
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers