10th Computer Notes Chapter 3
Chapter 3: Input and Output Handling

Write short answers to the following questions
i. Why format specifier is used? Explain with examples
Ans. FORMAT SPECIFIERS
A format specifier is computer code that tells about the data type, field width and the format according to which a value is to be printed or read from an input device.
For example, the format specifier %d is used to read or print a decimal integer, and the format specifier %ld is used for long integers.
ii. Why escape sequence is used? Explain with examples
Ans. The special characters used in C language to control printing on the output device are called escape sequences. These characters are not printed.
These are used inside the control string. An escape sequence is a combination of a backslash and a code character. For example, \b is used to move the cursor backwards by one position and \n is used to move the cursor to the beginning of the next line.
iii. What is the purpose of printf() function? Explain with an example.
Answer: The printf() function is used to print text, constants, values of variables and expressions on the screen in a specified format. The general syntax of this function is:
printf(control_string, list of arguments);
The ‘control_string’ consists of text, format specifiers and escape sequences. It is written within double quotes. The ‘list of arguments’ consists of a list of variables, constants and arithmetic expressions, separated by commas, whose values are to be printed.
Following is an example of printf() function.
printf(“\nThe value of a is %d and the value of b is %d.”,a ,b);
Assuming that a has the value of 3 and b has the value of 4, then the following message will be printed by the above statement on a new line.
The value of a is 3 and the value of b is 4.
iv. Differentiate between printf() and scanf() functions.
Answer: The printf() function is used to print text, constants, values of variables and expressions on the screen in a specified format.
The scanf() function is used to get values into variables from the keyboard during the execution of a program.
v. Evaluate the following expressions.
a) 7+5*(3+4)
=7+5*7
=7+35
=42
b) 100/10/4
=10/4
=2
c) 50%13%3
=11%3
=2
d) 30/7*3-6
=4*3-6
=12-6
=6
vi. What will be the output of the following program?
#include<stdio.h>
main()
{
int x,y,z1,z2,z3,z4;
x=17;
y=5;
z1=x/y;
printf(“\nz1=%d”, z1);
z2=x%y;
printf(“\nz2=%d”, z2);
z3=++ x;
printf(“\nz3=%d”, z3);
z4=y++;
printf(“\nz4=%d”, z4);
}
Ans: Output:
z1=3
z2=2
z3=18
z4=5
vii. What will be the output of the following program?
# include <stdio.h>
main()
{
int b;
float a,c,d,e,f;
a=14.84;
b=7;
c=a-b;
printf(“\nc=%f”,c);
d=a/b;
printf(“\nd=%f”,d);
e=a-b*3;
printf(“\ne=%f”,e);
f=(a+b)/2;
printf(“\nf=%f”,f);
}
Ans: Output:
c=7.840000
d=2.120000
e=6.160000
f=10.920000
Extensive Questions
Q1. Describe how basic and compound assignment operators are used.
Basic Assignment Operator
The basic assignment operator is =. This is used to assign the value of an expression to a variable. It has the general form:
variable = expression
where an expression may be a constant, another variable to which a value has previously been assigned or a formula to be evaluated. For example:
sum =a + b;
Compound Assignment Operators
In addition to =, there are a number of assignment operators unique to C. These include +=, =, /= and %=. Suppose op represents an arithmetic operator. Then, the compound assignment operator has the following general form to assign a value of an expression to a variable.
variable op = expression
This is equivalent to:
variable = variable op expression
For example, consider the following statement:
sum = sum +n;
This assignment statement could be written using a compound assignment operator as: sum += n;
The effect is exactly the same but the expression is more compact. Some more examples are
sum -=n is equivalent to sum = sum-n
prod *= n is equivalent to prod = prod * n
a/=b is equivalent to a=a/b
a%=b is equivalent to a=a%b
Q2. Describe the functions of the following operators.
i) Relational operators
ii) Logical operators
iii) Conditional operator
i) RELATIONAL OPERATORS
Relational operators are used to compare two values of the same type. These are used in expressions when a decision is to be based on a condition. After evaluation of an expression, the result produced is either True or False. Relational Operators are used in programming for decision-making.
ii) LOGICAL OPERATORS
Logical operators are used for building compound conditions. We have seen before that a single condition is built using a relational operator in an expression. If we need to build more than one condition for some action to take place in programming, then we have to form a compound condition.
TYPES OF LOGICAL OPERATORS
There are three types of logical operators.
| Operator | Definition |
| && | AND |
| || | OR |
| ! | NOT |
iii) CONDITIONAL (TERNARY) OPERATOR
A conditional operator is a decision-making operator. It has the following form.
Condition? expression1: expression2;
When this statement is executed, the condition is evaluated. If it is true, the entire conditional expression takes on the value of expression1. If it is false, the conditional expression takes on the value of expression 2. The entire conditional expression takes on a value and can therefore be used in an assignment statement. Consider the following example.
a = (k>15)? x*y : x+y;
This statement will assign the product of x and y to the variable a, if k is greater than 15 otherwise a will be assigned the sum of x and y.
Q3. Write a program that reads three numbers and prints their sum, product and average.
Ans.
#include<stdio.h>
#include<conio.h>
void main (void)
{
int a,b,c, sum, prod,avg;
printf (“Enter the first number:”);
scanf (“%d”, &a);
printf (“Enter the second number:”);
scanf (“%d”, &b);
printf (“Enter the third number:”);
scanf (“%d”, &c);
sum=a+b+c;
printf(“ The sum is %d”,sum);
prod=a*b*c;
printf(“ The product is %d”,prod);
avg=sum/3;
printf(“ The average is %d”,avg);
getch();
}
Q4. Write a program that reads the length and width of a rectangle and prints its area.
Ans:
# include <stdio.h>
#include<conio.h>
void main(void)
{
int l,w,a;
printf(“Enter the length of rectangle:”);
scanf(“%d”,&l);
printf(“Enter the width of rectangle:”);
scanf(“%d”,&w);
a=l*w;
printf(“Area of rectangle is %d”,a);
getch();
}
Q5. Write a program that reads the length of one side of a cube and prints its volume.
Ans.
# include <stdio.h>
#include<conio.h>
void main(void)
{
int l,v;
printf(“Enter one side of a cube:”);
scanf(“%d”,&l);
v=l*l*l;
printf(“Volume of cube is %d”,v);
getch();
}
Q6. Write a program that reads the temperature in Celsius, converts it into Fahrenheit and prints it on the screen.
Ans.
# include <stdio.h>
#include<conio.h>
void main(void)
{
int c,f;
printf(“Enter temperature in Celsius:”);
scanf(“%d”,&c);
f=1.8*c+32;
printf(“Temperature in Fahrenheit is %d”,f);
getch();
}
Q7. Write a program that reads the name and address of a person and prints it on the screen using gets() and puts()functions.
Ans.
#include <stdio.h>
#include<conio.h>
void main(void)
{
char name[20], address[20];
printf(“Enter the name of a person:”);
gets(name);
puts(name);
printf(“Enter the address of a person:”);
gets(address);
puts(address);
getch();
}
Select the best answer for the following MCQs.
i. Which function is used to output a single character on the screen?
A. printf()
B. putchar()
C. puts()
D. getche()
ii. Which function reads a single character the instant it is typed, without waiting for the enter key to be pressed and displays it on the screen?
A. scanf()
B. gets()
C. getch()
D. getche()
iii. Which character terminates a C statement?
A. Colon
B. Semicolon
C. Period
D. Comma
iv. Which format specifier is used to print or read a floating-point value in decimal notation?
A. %d
B. %g
C. %f
D. %e
v. Which escape sequence is used to move the cursor to the beginning of the current line?
A. \a
B. \r
C. \n
D.\b
vi. Which of the following is an arithmetic operator?
A. %
B. <=
C. &&
D. +=
vii. Which of the following is a logical operator?
A. %
B. <=
C. &&
D. +=
viii. Which statement is equivalent to “k=k+a;”
A. k+=a;
B. k=+a;
C. k++a;
D. k=a++;
ix. Which of the following is an increment operator?
A. +
B.+=
C. ++
D.=+
x. Which of the following operators has the highest precedence?
A. &&
B. <=
C. =
D. *