Chapter-3-Computer Science-10 Class-Federal Board
Chapter-3-Computer Science-10 Class-Federal Board
Chapter-3-Computer Science-10 Class-Federal Board
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:
The ‘list of arguments’ consists of a list of variables, constants and arithmetic expressions,
separated by commas, whose values are to be printed. The values are printed according to the
corresponding format specifier. The first format specifier applies to the first argument, the second
to the second argument and so on. The arguments in the printf() function are optional. When
printf() function is used to print only a message, then the arguments are omitted.
The format specifiers %d and %i are same when they are used with the printf()
function but different when used with scanf() function. For printf() function, both %d and
%i are used for decimal integer as shown in the program.
The execution of the program is shown in Fig.3-13
54
In this program when x/y is performed it will give an integer result because the fractional
part is truncated when the two operands are of type integer. Moreover, the remainder operator
will give the remainder after dividing x by y when x%y is performed. The output of the program
is shown in Fig.3-23.
variable op = expression
This is equivalent to:
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:
Oper Definition
ator
3 Input and Output Handling 61
== equal to
62
!= not equal to
3 Input and Output Handling 63
64
3 Input and Output Handling 65
Example:
(n<10)||(n>25)
Suppose, the value of n is 5, then the expression will be considered true because one of the
two conditions is true. If the value of n is 28 then also the compound condition will be true. If the
value of n is 12 then the expression will be false since both conditions are false.
The next compound condition will be true if a is greater than b or c is equal to 10. It will also be
true if both conditions are true, that is, a is greater than b and c is equal to 10. It will only be false
if a is not greater than b and at the same time c is not equal to 10.
The logical NOT operator is used with a single expression (condition) and evaluates to true if
the expression is false and evaluates to false if the expression is true. In other words, it reverses
the result of a single expression.
Syntax:
!Expression1
Truth table for AND operator is shown here under:
Expression !Expression For example, the expression:
True False
!(a<b)
False True will be true if a is not less than b. In
other words, the condition will be true if a is greater than or equal to b. The same condition can
also be written as given below which is easy to understand.
(a>=b)
3.2.5 INCREMENT AND DECREMENT OPERATORS
Increment operator is ++ and decrement operator is - -. These are defined in Fig.3-24.
Operator Definition
++ Increment by 1
-- Decrement by 1
In certain situations, ++n and n++ have different effect. This is because ++n increments
n before using its value whereas n++ increments n after it is used.
As an example, suppose n has the value 3. The statement: a = ++n; will first increment
n and then assigns the value 4 to a. But the statement: a = n++; will first assign the value
3 to a and then increments n to 4. In both cases n has the value 4.
For example:
a=5;
c=b; z=x+y;
In the above statements, variable a is assigned the value 5, c is assigned the value stored
in variable b and z is assigned the sum of values stored in variable x and y.
The relational operator (= =) is used to build a condition based on which computer takes some
action.
For example:
a= =1
c= = a+b
In the first condition, if the value of a is equal to 1 then the condition is true otherwise it is false.
In the second condition, the equal to operator is used to check whether the value of c is equal
to the sum of a and b. If it is equal then the condition is true otherwise it is false.
the logical operator ! (NOT). Binary operators are -, +, *, \, % and logical operators && (AND)
and || (OR).
a= -
b; k++;
- -x;
Some examples of binary operators are:
a=b+c;
z=x*y;
k=d%e;
3.2.8 CONDITIONAL (TERNARY) OPERATOR
A conditional operator is a decision-making operator. It has the following form.
Some programmers may prefer to use the above if-else statement rather than using the
conditional operator because it is easy to understand.
68 3 Input and Output Handling
The program in Fig.3-27, demonstrate the use of conditional operator for finding the larger
of two numbers.
62 + 3
and then the addition is carried out, giving the final result 65.
The standard order of evaluation can be modified by using brackets to enclose part of an
expression. The part of the expression within brackets is first evaluated in the standard manner
and then the result is combined to evaluate the complete expression. For example in the
expression, a / (b + c) b+c will be performed first and then a will be divided by the sum of b and
c.
If the brackets are nested, that is, if one set of brackets is contained within another, the
computations in the innermost brackets are performed first.
Key Points
• The printf() functions is used to print text and values on the screen in a specified format.
• The scanf() function is used to get values into variables from the keyboard during execution
of a program.
• A format specifier 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.
• Escape sequence is a combination of backslash (\) and a code character to control printing
of data on the screen.
• Relational operator is used to compare two values of the same type. It is used in an
expression when a decision is to be based on a condition in a program.
• The order of precedence of operators is the rule that specifies the order in which operations
are to be performed in an expression.
Exercise
3 Input and Output Handling 71
Short Questions
Q2. Give short answers to the following questions.
i. Why format specifier is used? Explain with examples.
70 4 Conditional Control Structure 1
3 Input and Output Handling
ii. Why escape sequence is used? Explain with examples.
iii. What is the purpose of printf() function? Explain with an example.
iv. Differentiate between printf() and scanf() functions.
v. Evaluate the following expressions.
a) 7+5*(3+4)
b) 100/10/4
c) 50%13%3
d) 30/7*3-6
vi. What will be the output of the following program?
# include <stdio.h>
void main(void)
{
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);
}
vii. What will be the output of the following program?
# include <stdio.h>
void main(void)
{
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);
2 3 Input and Output Handling
f=(a+b)/2;
printf(“\nf=%f”,f);
}
Extensive Questions
Q3. Describe how basic and compound assignment operators are used?
Q4. Describe the functions of the following operators?
i) Relational operators
ii) Logical operators
iii) Conditional operator
Q5 . Write a program that reads three numbers and prints their sum, product and average.
Q6. Write a program that reads the length and width of a rectangle and prints its area.
Q7. Write a program that reads the length of one side of a cube and prints its volume.
Q8. Write a program that reads temperature in Celsius, converts it into Fahrenheit and prints
on the screen.