Grade 10 Chapter 3
Grade 10 Chapter 3
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.
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
#include <stdio.h>
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
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
Variable op = expression
This is equivalent to:
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 = sum – n
prod = prod * n
a=a/b
a=a%b
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
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
Syntax
Expression1 && Expression2
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‟)
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.
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);
}
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