0% found this document useful (0 votes)
20 views11 pages

Grade 10 Chapter 3

CLASS 10TH COMPUTER CHAPTER3 Short questions

Uploaded by

ummefatima683200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Grade 10 Chapter 3

CLASS 10TH COMPUTER CHAPTER3 Short questions

Uploaded by

ummefatima683200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Grade 10 Chapter 3

Input and output handling Pakistan International School Al Azizia, Jeddah

Input and Output handling


Chapter 3
Short Questions
Q1.Why format specifier is used? Explain with examples.
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. Following are the
commonly used format specifiers:
%d = Decimal Integer
%i = integer
%ld = long decimal integer
%f = floating-point (decimal notation)
%g = floating-point (exponential notation)
%e = floating-point (%f or %g, whichever is short r)
%c = single character
%s = string

Q2.Why escape sequence is used? Explain with examples.


The special characters used in C language to control printing on the output device are called
escape sequences. These characters are not printed. An escape sequence is a combination of a
backslash (\) and a code character. The backslash is called the control character. A list of
commonly used escape sequences is given below:

Escape Sequence Meaning


\a Produces alert (be ) sound
\b Moves cursor backward by one position
\n Moves cursor to the beginning of next line
\r Moves cursor to the beginning of current line
\t \Moves cursor to the next horizontal tabular position
\\ Produces a backslash
\’ Produces a single quote
\” Produces a double quote
\? Produces a question mark
1
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah

Q3. What is the purpose of gets() function? Explain with an example.


The gets() functions is used to read a string from the keyboard and store it in the variable
specified inside the parenthesis. The program given below demonstrates the use of gets()
function in a program.

In this program, the format specifier %s is used to print the string stored in the variable name.
The execution of this is shown in Fig.

Q4.Differentiate between getch() and getche() functions.


 Sometimes in programming, it is required to read a single character as soon as it is
typed, without waiting for Enter key to be pressed.

 For example, in a game the user might want an object to move each time he presses
one of the arrow keys.

 It would be awkward to press the Enter key each time the user presses and arrow key.
The getche() function is used for this purpose. The get means it gets something from an
input device. The “ch‟ means it gets a character and the “e‟ means it echoes (displays)
the reads a single character to the screen when it is typed.

 The program in Fig below reads a single character the instant it is typed and displays it
on the screen. The user does not have to press the Enter key after typing the letter.

 In this program, the getche() function reads a single character from the keyboard and it
is assigned to the variable ch.

 The format specifier %c is used to print the letter stored in variable ch. The execution of
this program .

 The getch() is a similar function to getche(), the difference is that it does not display the
typed character on the screen.

 The getche() and getch() functions require conio.h header file, so it must be

included in the program as well.

Q6.What will be the output of the following program?

#include <stdio.h>

Void main (void)

2
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
int x,y,z1,z2,z3,z4;

x=17;

y=5; z1=x/y;

printf(“\n z1=%d”,z1);

z2=x%y ;

printf(“\nz2=%d”,z2);

z3=++x;

printf(“\nz2=%d”,z3);

z4=y++;

printf(“\nz4=%d”,z4);

Ans: Output:

Z1 = 3

Z2 = 2

Z3 = 18

Z4 = 5

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);

3
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
f=(a+b)/2;
printf(“\nf=%f”,f);
}
Ans:
Output: C= 7.840000
D = 2.120000
E = 6.160000
F = 10.920000

EXTENSIVE QUESTION
Q1.How basic and compound assignment operators are used?
They are used to assign values to variables used in computer programs.
C language provides three types of assignment operators.
These are
 Basic assignments operator
 Compound assignment operators
 Increment/decrement operators

Basic Assignment Operator:


 The basic assignment operator is =.
 This is used to assign value of an expression to a variable. It has the general
form: Variable = expression
 Where expressions 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 to assign value of an expression to a variable.

Variable op = expression
This is equivalent to:

Variable = variable op expression


For example, consider the following statement:

4
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah

Sum = sum + n;
This assignment statement could be written u ing 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 Prod*= n is equivalent to


A /= b is equivalent to A %= b is equivalent to

sum = sum – n
prod = prod * n
a=a/b
a=a%b

Q2.Describe the functions of the following operators?


i) Relational perators
ii) Logical operators
iii) Conditional operator

Relational Operators:
 These 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 a relational expression, the result produced is either True
of False.
 Relational operators are used in programming for decision making.
 There are six types of relational operators in C language. These are described
in table below

Operator Definition
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

Following are some examples of relational operators:


5
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
c>=a+b
x<5.3
n==20
count!=10
 If a has the value 25, b has the value 10 and c the value 28 then first expression is false
since 28 is not greater than or equal to 35.
 In the second expression, if x has the value 4.5, the expression is true because x is less
than 5.3.
 In the last expression if count is any number other than 10 then the expression will be
true. It will only be false when count is equal to 10.

B) Logical operators:
 Logical operators are used for building compound conditions.
 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 programming,
then we have to form compound condition.
 They have following types:

Operator Definition
&& AND
|| OR
! NOT

Operator Definition && AND | | OR ! NOT


Logical AND (&&) Operator:
It is used to form compound condition in which two relational expressions are evaluated. One
relational expression is to the left and the other to the right of the compound condition is
considered true otherwise it is false.

Syntax
Expression1 && Expression2

Truth table for AND operator is shown here under.


Expression-1 Expression-2 Expression-1 && Expression-2
True True True
True False False
False True False

6
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
False False False

Example:
Consider the following compound condition:
(a>=1)&&(a<=10)

 This expression is considered true if both conditions (a>=1) and (a<=1) are true, that is, if the
value of a is between 1 to 10.

 In the next example, the compound condition is considered true if the value stored in
a is greater than the value stored in b and the value stored in c is equal to 15. (a>b)&&(c==15)

 The following compound condition will check whether the character stored in variable ch is a
lowercase letter or not. (ch >= ‟a‟ ) && (ch<= “z‟)

Logical OR (||) Operator:


Logical OR operator is also used to form compound condition just like the logical AND
operator, one relational expression is to the eft and the other to the right of the OR operator.
The compound condition is true if either of the conditions is true or both conditions are true. It
is considered false only if both of the conditions are false.
Syntax:
Expression1|| Expression2
Truth table for AND operator is shown here under:
Expression-1 Expression-2 Expression-1 && Expression-2
True True True
True False True
False True True
False False False

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.

7
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah

The next compound conditions will be true if a is greater 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 not equal to 10.

(a>b) || (c==10)
Logical OR condition is used when we wish to perform an operations if one of the two
conditions is true or both of the conditions are true.

Logical NOT (!) Operator:


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
True False
False True
For example, the expression:
!(a<b)

C) Conditional Operator/ Conditional (Ternary) Operator:


A conditional operator is a decision-making operator. It has the following form.

Condtion ?expression1: expression2:


When this statement is executed, the condition is evaluated.
If it is true, the entire conditional expression takes n the value of expression1.
If it is false, the expression takes on a value and can therefore be used in assignment
statements.
Consider the following example. A= (k>15)? x*y : x+y;

8
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
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.
this expression is equivalent to the following if-else statement which will be explained in the
next unit.
If (k>15)
A = x*y; Else A = x+y;
Some programmers may prefer to use the above if-else statement rather than using the
conditional operator because it is easy to understand.
The program in Fig, demonstrate the use of conditional operator for finding the larger of two
numbers.

Q3.Write a program that reads three numbers and prints their sum,
product and average.
#include <stdio.h>
#include<conio.h>
int main()
{
int num1, num2, num3, sum, prod, avg ;
printf(“Enter the three numbers \n”);
scanf(“ %d, %d, %d ” , & num1, &num2, &num3);
sum = num1 + num2 + num3;
prod = num1 * num2 * num3;
avg = sum/3;
printf(“the sum of the three numbers is : %d ”,sum);
printf(“the prod of the three numbers is : %d”, prod);
printf(“the average of the three numbers is : %d”, avg);
getch();
}

Q4.Write a program that reads the length and width of a rectangle and
prints its area.
#include <stdio.h>
#include <conio.h>

9
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
void main()
{
float length, width, area;
printf(“Enter length of rectangle:”);
scanf(“%f”, &length);
printf(“Enter width of rectangle:”);
scanf(“%f”, &width);
area= length*width;
printf(“Area of rectangle - %f sq. units “, area);
}

Q5.Write a program that reads the length of one side of a cube and
prints its volume.
#include <stdio.h>
#include <conio.h>
void main()
{
float side, volume;
printf(“Enter length of any side of cube\n”);
scanf(“%f”, &side);
volume=side*side*side;
printf(“Volume of Cube: %0.4f “, volume);
}

Q6.Write a program that reads temperature in Celsius, Converts it into


Fahrenheit and prints on the screen.
#include <stdio.h>
#include <conio.h>
void main()
{

10
Grade 10 Chapter 3
Input and output handling Pakistan International School Al Azizia, Jeddah
float fahren, celsius;
printf(“Enter the temperature in celsius\n”);
scanf(“%f”, &celsius);
fahren =(9.0/5.0)*celsius + 32;
prinft(“%.2f C is equal to %.2f F ”, celsius, fahren);
}

Q7.Write a program that reads name and address of a person and prints it on
the screen using gets() and puts() functions.
#include <stdio.h>
#include <string h>
int main()
{
char name[50], add[50];
printf(“Enter your name:”);
gets(name);
printf(“Enter your address:”);
gets(add);
printf(“Your name is:”);
puts(name);
printf(“Your address is:”);
puts(add);
}

11

You might also like