Input Output C Programs. This post is for ICS Part-II | 2nd Year Computer Science Chapter No.10. Input/Output. The following programs along with answers are important for the final exams.
Chapter No.10: Input/Output
Exercise Programs in C
Q.8(a): Show how the value -17.246 would be printed using the formats
%8.4f, %8.3f, %8.2f, %8.1f, %8.0f, and %0.2f.
Answer:
%8.4f -17.2460
%8.3f □-17.246
%8.2f □□-17.25
%8.1f □□□-17.2
%8.0f □□□□□-17
%0.2f □□-17.25
The following figure shows how the value -17.246 would be printed using the formats %8.4f, %8.3f, %8.2f, %8.1f, %8.0f, and %0.2f.
Q.8(b): Assuming x (type double) is 21.335 and y (type int) is 200, show the
output of the following statements (on paper). For clarity, use the symbol □
shows a blank space).
Answer:
Output: x is □21.34 y is □200
printf("y is %d\n",y);
Output: y is 200
printf("x is %.1f\n",x);
Output: x is 21.3
The following figure shows the output screen of the above-mentioned statements:
Q.8(c): If the variables a, b, and c are 307, 408.558, and -12.31,
respectively, write a statement that will display the following line: (for
clarity, the symbol □ shows a blank space).
□□307□□□□□408.56□□□□-12.3
Answer:
Following is the code that will display the above line/output:
printf("%5d%11.2f%9.1f",a,b,c);
Q.9: Write a program that asks the user to enter the radius of a circle and then computes and displays the circle’s area. Use the
formula.
area = PI x radius x radius
Answer:
#include<stdio.h>
#include<math.h>
#define PI 3.14159
void main()
{
float radius,area;
printf("Enter the
radius of circle:");
scanf("%f",&radius);
area=PI*radius*radius;
printf("Area of
Circle is =%.2f",area);
getch();
}
Q.10: Write a program that stores the values ‘A’, ‘U’, 3.456E10, and 50 in
separate memory cells. Your program should get the first three values as input
data but use an assignment statement to store the last value.
Answer:
#include<stdio.h>
#include<conio.h>
main()
{
char ch1,ch2;
float a;
int b;
// INPUT STATEMENTS
printf("Enter
1st Character:");
ch1=getche();
printf("\nEnter
2nd Character:");
ch2=getche();
printf("\nEnter
Exponential Value:");
scanf("%e",&a); // 3rd Value
b=50; // 4th Value
// OUTPUT STATEMENTS
printf("\n\n====OUTPUT=====");
printf("\n\n
1st Character:%c",ch1);
printf("\n2nd
Character:%c",ch2);
printf("\n1st
Value:%e",a);
printf("\n2nd
Value:%d\n\n",b);
getch();
}
Q.11: Write a that converts a temperature in degrees Fahrenheit to degrees
Celsius. For conversion, use the following formula.
celsius = 5/9
(Fahrenheit – 32)
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
float
celsius,fahrenheit;
printf("Enter the temperature in
Fahrenheit=");
scanf("%f",&fahrenheit);
celsius=5.0/9.0*(fahrenheit-32.0);
printf("Temperature in Celsius = %.2f",celsius);
getch();
}
Q.12: Write a program that takes a positive number with a fractional part and rounds it to
two decimal places. For example, 25.4851 would round to 25.49, and 62.4431
would round to 62.44.
Answer:
#include<stdio.h>
#include<conio.h>
main()
{
float a;
printf("Enter any
number:");
scanf("%f",&a);
printf("\nNumber with two
decimal places:%.2f",a);
getch();
}
Post a Comment
Your feedback is highly appreciated and will help us to improve. So, please give your good suggestions.