0% found this document useful (0 votes)
110 views47 pages

Computer 10th Fbise Solved Notes by Encore Star College & Academy 03064941878

The document provides an overview of programming techniques for 10th-grade computer science, including definitions of key concepts such as computers, algorithms, and flowcharts. It outlines the steps involved in problem-solving and includes algorithms for various tasks, such as calculating areas and generating multiplication tables. Additionally, it discusses programming languages, their characteristics, and the differences between assembly languages and high-level languages.

Uploaded by

alihamzaazam.pk
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)
110 views47 pages

Computer 10th Fbise Solved Notes by Encore Star College & Academy 03064941878

The document provides an overview of programming techniques for 10th-grade computer science, including definitions of key concepts such as computers, algorithms, and flowcharts. It outlines the steps involved in problem-solving and includes algorithms for various tasks, such as calculating areas and generating multiplication tables. Additionally, it discusses programming languages, their characteristics, and the differences between assembly languages and high-level languages.

Uploaded by

alihamzaazam.pk
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/ 47

ENCORE STAR COLLEGE & ACADEMY

COMPUTER SCIENCE 10TH CLASS

CH-1: PROGRAMMING TECHNIQUES

Q2. Give short answers to the following questions.


i. Define computer.
Ans: A computer is a general-purpose electronic machine invented to help people solve various problems. Computer
must be programmed by human beings to perform various tasks. Various programming techniques are used for
solving problems on computer.

ii. What is algorithm and what is the role of algorithm in problem solving?
Ans: Algorithm means method, procedure, technique or plan. Algorithm is a step by step problem solving method that
is easy to understand and follow. It is a set of steps that clearly defines a sequence of operations to solve a problem.

Role of algorithm in problem solving:


Algorithm plays an important role in computer programming. Computer programming is the process of taking an
algorithm and coding it in a programming language. Formulating an algorithm is the first step for developing a
computer program.
iii. What is a flowchart?
Ans: Flowchart is a diagrammatic representation of algorithm. It describes what operations required to solve a given
problem.

iv. What are the advantages of using flowcharts?


• Flowchart illustrates the sequence of operations to be performed to solve a problem in the form of a diagram.
• Computer programmers draw flowcharts before writing computer programs. It provides an easy way to
analyze and find solutions of problems.
• Once, the flowchart is drawn, it becomes very easy to write the program in any high level language.
• It is very helpful in communicating the problem solving method to other people. It also helps in finding and
removing errors in computer programs.

v. Draw any four graphical symbols used in flowchart and explain them.
a. Flow Line:
It is a line with arrow head used to connect various flowcharts symbols and indicates the flow of
Control in the flowchart.

b. Start/Stop Symbol:
It is rounded rectangular shaped symbol .It is used to indicate the start or end of a flowchart. We
can write the words Start or Stop inside this symbol. A flowchart only can have one start but it may have many
ends.

ENCORE STAR COLLEGE +92-306-4941878 Page 1


ENCORE STAR COLLEGE & ACADEMY
c. Input/output Symbol:
Parallelogram represents input or output operations in a flowchart. It contains the word READ or
INPUT along with the variables for input operations or PRINT or OUTPUT along with the output data for output
operation.

d. Process Symbol:
A rectangular block is used for any data processing operation. All the calculations appear inside
the processing symbol, such as “SUM=A+B”. Variables are also initialized inside the process
symbol, such as “K=1”.

e. Decision Symbol:
A diamond shaped symbol represents decision in a flowchart and it contains a condition. If the
condition is true, the path marked YES is to be followed. If the condition is false, the path marked
NO is to be followed. The words TRUE or FALSE can also be used instead of YES or NO.

f. Connector Symbols:
These symbols are used to connect one part of a flowchart to the other
on the same page( On-Page connector) or on the new page (Off-Page connector)

Q3. Describe the steps involved in problem solving.

Ans: there are five steps are involved in problem solving on the computer.

i. Defining the problem:


Defining the problem is initial stage of problem solving. It is very important to understand the problem
before the programmer starts working on its solution.
The following are the steps to properly define and understand the problem.
• Carefully read the problem to understand what it tells.
• Find out what the problem asks to do.
• What information can be obtained as the solution of the problem?
• What is required to be calculated as the solution of the problem?
Example,
Problem: Finding average marks of a student
It is clear from the problem statement that average marks of a student have to be found. This is
numeric problem.
ii. Analyzing the problem:
At this stage of problem solving, the programmer investigates the problem and gathers as much
information as possible to find a solution.
The following questions are to be asked to analyze the problem.
• Is it possible to solve the problem on a computer?
• What is to be done to find the solution of the problem?
• What is the proper sequence of steps to solve the problem?

ENCORE STAR COLLEGE +92-306-4941878 Page 2


ENCORE STAR COLLEGE & ACADEMY
• What are the inputs and what output is required?
• How many solutions are possible?
• Which solution is the best and why?
• How solution will be implemented?

iii. Planning the solution of the problem


Planning the solution of the problem is a creative stage of problem solving. It refers to dividing the
solution into steps and arranging them into proper order that will solve the problem.
Example,
Problem: Finding average marks of a student
To solve this problem following planning is required.
• All subject marks are to be added together to find the sum of all the marks.
• The sum is to be divided by the total number of subjects to find the average marks.

iv. A candid solutions of a problem


All the possible solutions of a problem that produce correct result are known as candid solutions. To find
candid solutions of a problem, programmer has to look for different methods to solve the problem and
come up with several solutions.

v. Select the best solution


After finding the candid solutions, only one solution can be selected. The selection of final solution of a
problem should be based on following criteria.
• Speed: the program should run fast
• Cost: the program should be cost-effective way of implementation.
• Complexity: The program should not be complicated and contain minimum instructions and
simple steps.

Q4. Write an algorithm to calculate the area of a rectangle for given breadth and length.
Ans: Step 1: Start
Input the Breadth (B) and Length (L) of a rectangle
Step 2: Calculate the area (A) by multiplying L with B
Step 3: Print A
Step 4: Stop
Q5. Write an algorithm that inputs length in inches and calculate and print it in centimeters.
Ans: Step 1: Start
Input the Length in inches (LI)
Step 2: Calculate the length in cm (LCM) by multiplying LI with 2.54
Step 3: Print LCM
Step 4: Stop
Q6. Write an algorithm that inputs marks and prints the message “PASS” or “FAIL”. Passing marks are 33.
Ans: Step 1: Start
Input Marks (M)
Step 2: Check if (M<33) then Print “FAIL” otherwise GOTO Step 3
Step 3: ELSE Print “PASS”

ENCORE STAR COLLEGE +92-306-4941878 Page 3


ENCORE STAR COLLEGE & ACADEMY
Step 4: Stop
Q7. Write an algorithm to find the sum of given sequence.
SUM = 20 + 25 + 30 + 35 + 40 + 45 + 50 + 55 + 60
Step 1: Start
Initialize SUM=0 and K = 20
SUM=0, K=20
Step 2: ADD K to SUM
SUM=SUM+K
Step 3: Increment K by 5
K=K+5
Step 4: Check if the value of K is less than or equal to 60
IF K≤60 THEN GOTO Step 2 otherwise GOTO Step 5
Step 5: Print SUM
Step 6: Stop

Q8. Write an algorithm to find the product of given numbers.

PRODUCT = 1 x 3 x 5 x 7 x 9 x 11 x 13 x 15

Step 1: Start
Initialize variable K to 1 and Prod =1
K= 1 Prod= 1
Step 2: Increment K by 2
K=K+2
Step 3: Find the product
Product= Prod x K
Step 4: Check if the value of K is less than 16
IF K<16 THEN GOTO Step 2 otherwise GOTO Step 5
Step 5: Print Product
Step 6: Stop

Q9. Write an algorithm to print multiplication table of a number in reverse order.

Step 1: Start
Enter the Number N whose table is to be generated
Step 2: Initialize the value of I with 10

ENCORE STAR COLLEGE +92-306-4941878 Page 4


ENCORE STAR COLLEGE & ACADEMY
I=10
Step 3: Find the product of N and I
Prod=N x I
Step 4: Print N, I and Prod
Step 5: Decrease the value of I by 1
I=I-1
Step 6: If the value of I is > 0 then goto step 3 otherwise Goto Step 7
Step 7: Stop
Q10. Convert the algorithms of questions Q4 to Q9 to flowcharts.

i. Convert flowchart of an algorithm to calculate the area of a rectangle for given breadth and length.

START

INPUT L, B

AREA=L * B

PRINT AREA

STOP

ENCORE STAR COLLEGE +92-306-4941878 Page 5


ENCORE STAR COLLEGE & ACADEMY
ii. Convert flowchart of an algorithm that inputs length in inches and calculate and print it in centimeters.

START

INPUT LENGTH LI (LEGNTH IN INCHES)

LCM=LI * 2.54

PRINT LCM

STOP
iii. Convert flowchart of an algorithm that inputs marks and prints the message “PASS” or “FAIL”. Passing
marks are 33.

START

INPUT MARKS (M)

YES

IF M<33
NO

PASS FAIL

STOP

ENCORE STAR COLLEGE +92-306-4941878 Page 6


ENCORE STAR COLLEGE & ACADEMY
iv. Convert flowchart of an algorithm to find the sum of given sequence.
SUM=20+25+30+35+40+45+50+55+60

START

SUM =0, K=20

SUM =SUM+K
K=K+5

YES NO
IF K<60

PRINT SUM

STOP
v. Convert flowchart of an algorithm to find the product of given numbers.
PRODUCT = 1 x 3 x 5 x 7 x 9 x 11 x 13 x 15

START

K =1, PROD=1

K =K+2
PROD=PROD *K

NO YES
IF K<16

PRINT SUM

STOP

ENCORE STAR COLLEGE +92-306-4941878 Page 7


ENCORE STAR COLLEGE & ACADEMY
vi. Convert flowchart of an algorithm to print multiplication table of a number in reverse order.

START

ENTER A NUMBER N

I=10

PROD=N * I

PRINT N, I, PROD

I=I-1

YES NO
IF PROD>0

STOP

---------------@@@ ENCORE STAR COLLEGE & ACADEMY @@@ ----------------

ENCORE STAR COLLEGE +92-306-4941878 Page 8


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
CH-2: PROGRAMMING IN C

Short Questions
Q2. Give short answers to the following questions.
i. Define computer program.
A computer program is a set of instructions (statement) written in a programming language to solve a particular
problem and achieving specific results. Any task performed by a computer is controlled by a set of instructions that are
executed by the microprocessor. A large variety of programming languages have been developed for writing computer
programs to use the computer as a problem-solving tool. Each statement of a programming language has syntax and
semantic.

ii. Difference between syntax and semantic.


SYNTAX SEMANTIC
1 Syntax refers to the rules of a programming Semantic gives meaning to statements of a
language according to which statements of a programming language.
program are to be written.
2 It describes the way to write correct statements It describes the sequence of operations to be
in a program. performed by a computer.
3 Syntax of programming language is similar to the Semantic define the meaning and sequence in a
grammar of a natural language. programming language.
4 Syntax of assignment statement The semantic of assignment statement
Variable=expression; Sum=a + b;

iii. Write three differences between assembly languages and HLLs.


Assembly Languages High Level Languages (HLLs)
1 Consists of symbolic codes or abbreviations Consists of English –oriented languages
known as mnemonics.
2 Programs are not portable. Programs are portable.
3 Less storage and less running time are required. More storage and more running time are required.
4 Gradually use is declining. Gradually use is increasing.
5 Complicated, difficult and more prone to errors Simple, easier and less prone to errors as compare to
as compare to HLLs. assembly languages.
6 It’s machine dependent. It’s not machine dependent.
7 Debugging is difficult as compared to HLLs. Debugging is easier than assembly languages.

iv. Write four characteristics of HLLs.


• Consists of English–oriented languages.
• Programs are portable & gradually use is increasing.
• More storage and more running time are required.
• Simple, easier and less prone to errors as compare to assembly languages.
• It’s not machine dependent. Debugging is easier than assembly languages.

ENCORE STAR COLLEGE & ACADEMY GHOURI TOWN +92-306-4941878 Page 1


ENCORE STAR COLLEGE & ACADEMY
v. Define Integrated Development Environment (IDE).
• Most of new programming languages use IDE to create, compile and run programs.
• IDE is comp software that brings all the processes & tools required for program development into one place.
• IDE’s aim is to make the life of programmers easier.
• Today’s modern IDEs have user-friendly Graphical User Interface (GUI).
vi. Difference between constant and variable.
Constant: Constants are quantities whose values do not change during program execution. It has three types.
1. Numeric Constants: are the two types.
a) Integer Constants: represent values that are counted, like the number of students in a class.
Some examples are 7145, -234, 26 etc.
b) Floating-point Constant: are used to represent values that are measured in decimal form.
Some examples are 166.75, 82.6 etc.
2. Character Constant: is one of the symbols in C character set. A character constant is enclosed by single
quotes such as ‘a’, ‘s’ etc
• It concludes digits 0 to 9.
• Upper case & lower case letters.
• Punctuation symbols such as semicolon (;) , comma(,), period (.) etc
• Special symbols such as +,-,=,> etc
3. String Constant: contains a string of characters within double quotes such as “Hello Ahmed”, “a” etc

Variable: is a symbolic name that represents a value that can change during execution of a program.
Variable are of two types.
1. Numeric Variables: are used to represent numeric values in computer programs. They represent integer
and floating-point values. Examples are sum, avg, length, salary, marks etc.
2. Character variables: represent character values in computer programs. It represent single character or a
string of characters. Examples are name, city, gender etc.
vii. Which of the following are valid C variables? Give the reason if not a valid variable.
Area, 5x, Sum, net pay, float, _ age, else, case, size 22, my_weight
Ans: Valid C variables: my-weight, area, size22, Sum
Not Valid C Variables Reason
5x A variable can’t be start with number.
net pay Space can’t be used in variable name.
float Data types can’t be used as variable name.
_age Special character (_) can’t be used in variable name.
else Commands cannot be used as variable name.
case Commands cannot be used as variable name.

viii. What are reserved words? Why they should not be used as variable names?
Reserved Words: The words that are part of programming language and have special purposes in computer programs
are called reserved words or keywords. Reserved words are always written in lowercase.
Why they should not be used as variable names?
Because they have predefined use and cannot be used for any other purpose or even as a variable. There are 32 words
defined as reserved words in C. examples are int, do, while, void, if, break etc

ENCORE STAR COLLEGE & ACADEMY GHOURI TOWN +92-306-4941878 Page 2


ENCORE STAR COLLEGE & ACADEMY
ix. Why comments are used in programs?
It is a good programming practice to add comments in program to make it easy for others to understand it. Comments
in the source code are ignored by the compiler. Comments are added in programs when a fact is necessary to be
brought to the attention and clarification of program reader. There are two types.
1. Single line comment: The // is used as single line comment in a source program.
2. Multiple line comment: The /*….*/ is used for multiple line comments.
x. What is the use of typecasting in C programs?
Typecasting is a method to convert a variable from one data type to another data type during program execution. It
makes a variable of one type to act like another type. Example, a variable of type int can act as a variable of type char
using typecasting. This is required in some situations in programming in C language. There are two types.
• Implicit typecasting
• Explicit typecasting
xi. What is the purpose of header files in C language?
C language contains a number of standard functions in library file that perform various tasks in C programs. These
tasks include all the input/output operations and all the math operations. Library file contains header file contains a
set of functions. Some commonly used header files are stdio.h, conio.h, and math.h.

Q3. Describe the following HLLs.

a) C / C++
C:
• Developed in early 1970s by Dennis Ritchie at Bell Lab.
• Become one of the most popular programming languages today.
• Highly structure, easy to understand and use.
• In the past, used for writing system programs such as OS, compilers, assemblers etc.
• Today, used for writing all types of application programs such as Office, educational & games software’s
.
C++:
• Developed by Bjarne Stroustrup at Bell Lab during 1983-1985.
• C++ is a superset of C, meaning that any valid C program is also a valid C++ program.
• The purpose was to provide programming facilities to easily and quickly write more powerful programs.
b) Visual Basic
• VB is a high level language which is evolved from the earlier version called BASIC.
• BASIC stands Beginner’s All- purpose Symbolic Instruction Code.
• VB is a very popular programming language for writing Windows and Web applications with graphical environment.
• VB is commonly used for developing business programs such as payroll system & inventory control program.
• The user can also write programs related with engineering, science, arts, education, games etc.

c) C#
• C# (pronounced as C-sharp) is a language developed in 2000 by Microsoft Corporation.
• It is simple, modern, general-purpose programming language.
• Syntax of C# is very similar to C and C++ and it also have some features of Java.
• Easy and efficient.
• It provides facilities to write Web applications that can be used across the internet.
• All types of programs including games, utilities, operating systems etc can be developed in C#.

ENCORE STAR COLLEGE & ACADEMY GHOURI TOWN +92-306-4941878 Page 3


ENCORE STAR COLLEGE & ACADEMY

d) Java
• Java is a high level language developed by Sun Microsystems.
• It is very similar in syntax to C and C++.
• In Java, the user can write all types of programs as those written in other programming languages and small
programs that can be embedded in a Web page accessed through Internet.
• Java is ideal language for network computing.
• It is used for writing programs for a wide range of devices, computers and networks.
• It is widely used in Web applications.
• The current versions of most of the Web browsers are made Java enabled like Microsoft internet explorer, Firefox
and Mozilla.
Q4. Differentiate between compiler and interpreter.

Compiler Interpreter
1 A compiler converts a program into machine code A interpreter converts a program into machine code as
as a whole at a time. line by line or as a single statement at a time.
2 Compiler creates object code file. Interpreter does not create object code file.
3 Program execution is fast. Program execution is slow.
4 Error detection and removal is difficult. Error detection and removal is simple and easy.
5 Memory requirement is more Memory requirement is less
6 Example: C, PASCAL, JAVA Example: BASIC, Java Script, Perl, VB

Q5. Describe the functions of linker and loader programs.


Linker: Linker is software that translates object program into a single executable program. During this process, if it could
not find the definition of a particular function that is used in the program, then it would assume that it is defined in C
library. It will replace this function in the object program with the code from C library and then create a single executable
program.
Loader: It is software that loads programs into memory and then executes them.

Q6. What are the rules for specifying a variable name in C language?

• A variable begins with a letter or underscore (_) and may consist of letters, underscores and/or digits.
• The underscore may be used to improve readability of the variable name. Example over_time.
• There is no restriction on the length of a variable name.
• Both upper and lower case letters are allowed in naming variables. An upper case letter considered different from a
lower case letter. Example the variable AVG is different from Avg or avg.
• Special characters cannot be used as variable name. e.g #, ?, @ etc.
• Reserved words of C language such as int, case, if, etc. cannot be used as variable names.
• There must be no embedded blank in the name of variable. Example m ass is not correct.

ENCORE STAR COLLEGE & ACADEMY GHOURI TOWN +92-306-4941878 Page 4


ENCORE STAR COLLEGE & ACADEMY
Q7. What is the difference between implicit type casting and explicit type casting? Give examples.

Implicit type casting Explicit type casting


1 Implicit typecasting is performed automatically by Explicit typecasting is performed with programmer’s
the compiler without programmer’s intervention intervention.
2 In this type of casting the compiler converts all The programmer explicitly defines the data type in
operands into the data type of the largest parenthesis before the variable or expression, as
operand. follows: (type) expression

3 Example: compiler converts the sum of two Example: the programmer converts the division of
float numbers to an int. two int numbers to float.
#include <stdio.h> #include <stdio.h>
void main (void) void main (void)
{ {
float value1=2.5; int value1=2.5;
float value2=5.3; int value2=5.3;
Int result; float result;
result=value1+value2; result=(float)value1+value2;
printf(“Result:%d”, result); printf(“Result:%f”, result);
} }

Q8. What is a preprocessor? Give examples.

• Preprocessor Directives are instructions for the C compiler.


• Every C language program certain preprocessor directives at the beginning of the program.
• Before translating a C language program into machine language, the compiler of C language carries out the
processor directives.
• These directives start with number sign (#).
• The most commonly used preprocessor directives are #include and #define.
➢ The include Preprocessor Directive
When this preprocessor is carried out by the C compiler, it will search for the header file that is written
within the less than (<) and greater than (>) symbols and copy it into the source file.
Syntax: #include<header file name>
➢ The define Preprocessor Directive
The define preprocessor is used for defining constants (i.e. Symbolic constants) in C programs.
Syntax: #define SYMBOL value/expression.
Here, SYMBOL is a valid C variable name and it is by convention written in uppercase.
Example #define HEIGHT 12
Q9. What is C language IDE? Explain its modules in detail.

• Most of new programming languages use IDE to create, compile and run programs.
• IDE is comp software that brings all the processes & tools required for program development into one place.
• IDE’s aim is to make the life of programmers easier.
• Today’s modern IDEs have user-friendly Graphical User Interface (GUI).

ENCORE STAR COLLEGE & ACADEMY GHOURI TOWN +92-306-4941878 Page 5


ENCORE STAR COLLEGE & ACADEMY
A C language IDE consists of the following modules.

• Text Editor: A text editor is a simple word-processor that is used to create and edit source code of a program. Files
created by a text editor are plain text files. Most of the editors automatically highlight compile errors to simplify
removing them.
• Compiler: A compiler is a software that translates C language program (source program) into machine code (object
program that can be understood and executed by the computer. It also detects syntax errors and gives hints to
programmer to correct them.
• Linker: Linker is a software that translates object program into a single executable program. During this process, if
it could not find the definition of a particular function that is used in the program, then it would assume that is
defined in C library. It will replace this function in the object program with the code from C library and then create a
single executable program.
• Loader: It is a software that loads programs into memory and then executes them.
• Debugger: It is a software that executes a program line by line, examines the values stored in variables and helps
in finding and removing errors in programs.

ENCORE STAR COLLEGE & ACADEMY GHOURI TOWN +92-306-4941878 Page 6


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
CH-3: INPUT AND OUTPUT HANDLING 10TH
Short Questions
Q2. Give short answers to the following questions.
i. 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 printed or read from an input device. A list of commonly used specifiers:

%d decimal integer
%i integer
%Id long decimal integer
%f floating-point (decimal notation)
%e floating-point (exponential notation)
%c single character
%s string
ii. Why escape sequence is used? Explain with examples.
The special characters used in C language to control printing on the output device called escape sequences.
These characters are not printed. These are used inside the control strings. 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.
\a Produces alert (bell) sound
\b Moves cursor background by one position
\n Moves cursor to the beginning of next line
\r Moves cursor to the beginning of the 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
The program given below demonstrates the use of escape sequence.
#include<stdio.h>
#include<conio.h>
void main(void)
{
printf(“\nRings bell three times.\a\a\a”);
printf(“\nRemoves 2 spaces between the name. sohail \b\bHeader”);
printf(“\n\nLeaves a blank line before printing this line.”);
Printf(“\n \rMoves this text to beginning of line..”);
printf(“\nIssues tab after each word. I\tam\tSohail”);
printf(“\nDisplays backslash symbol. \\”);
printf(“\nDisplays name is single quotes. \’Sohail\’ ”);
printf(“\nDisplays name is double quotes. \’Sohail\” ”);
printf(“\nDisplays question mark at the end. Who are you\?”);
getch();
}

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 1


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
iii. 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. Example given below
#include<stdio.h>
#include<conio.h>
void main(void)
{
char name [20];
printf(“\nEnter your name:”);
gets(name);
printf(‘\nMy name is %s”, name);
getch();
}
iv. Difference between getch ( ) and getche ( ) functions.
Sometimes in programming, it is required to read a single character the instant it is typed, without
waiting for Enter key to be pressed. It would be awkward to press the Enter key each time the user
presses an 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 character to the screen
when it is typed.
The getch() is similar function to getche(), both these two functions require conio.h header file, so it
must be included in the program as well.
The difference is that it does not display the typed character to the screen.
Program #include <stdio.h>
#include <conio.h>
void main(void)
{
char ch;
printf(“\nEnter a single character:’);
ch=getche();
printf(“\nThe character you typed is %c”, ch);
getch();
}
iii. What is the purpose of printf( ) function? Explain with an example.
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 lit of variables, constants and arithmetic expressions, separated by
commas, whose values are to be printed.
Example: printf(“\n The value of a is %d and the value of b is %d.”, a , b);
Assuming that a that has the value 3 and b has the value of 4. Message will be printed
The value of a is 3 and the value of b is 4

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 2


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
iv. Difference between printf ( ) and scanf ( ) functions.
Printf() Scanf()
The printf() function is used to print text, constants, The scanf() function is used to get values into
values of variables and expressions on the screen in variables from the keyboard during execution of a
a specified format. program. The value is input into a variable in a
specified format.

syntax : printf(“control_string, list of arguments”); Syntax: scanf(“format specifiers”, list of variables);

Example
#include<stdio.h>
#include<conio.h>
Void main(void)
{
int a, b, c;
printf(“Enter the first number:”);
scanf(“%d”, &a);
printf(“Enter the second number:”);
scanf(“%d”, &b);
c=a+b;
printf(“The sum is %d”, c);
getch();
}

v. Evaluate the following expressions.


a) 7+5*(3+4)
=7+5*7
=7+35
=42
b) 100/10/4
=10/4
=2.5
c) 50%13%3
=11%3
=2
d) 30/7*3-6
=4.28*3-6
=12.84-6
=6.84

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 3


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
vi. What will be the output of the following program?
# include <stdio.h>
# include <conio.h>
void main(void)
{
int x,y,z1,z2,z3,z4;
x=17; Output:
y=5;
printf(“\nz1=%d”, z1); Z1=3
z1=x/y;
printf(“\nz2=%d”, z2); Z2=2
z2=x%y; Z3=18
printf(“\nz3=%d”, z3);
z3=++x; Z4=5
printf(“\nz4=%d”, z4);
z4=y++;
getch();
}
vii. What will be the output of the following program?
# include<stdio.h>
#include<conio.h>
void main(void)
{
int b;
float a, c, d, e, f; Output:
a=14.84;
b=7; c=7.840000
c=a-b;
printf(“\nc=%f”, c); d=2.120000
d=a/b;
e=6.160000
printf(“\nd=%f”, d);
e=a-b*3; f=10.920000
printf(“\ne=%f, e);
f=(a+b)/2;
printf(“\nf=%f”, f);
getch();
}

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 4


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878

Q3. Describe how basic and compound assignment operators are used?
Basic Assignment Operators: 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 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 to assign value of an expression to a
variable.
Variable op=expression
Examples
sum=sum + n;
sum +=n;
Q4. Describe the functions of the following operators?
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 a relational expression, the result
produced is either True or False. Relational operators are used in programming for decision making.
Types of Relational Operators
Six types of relational operators are available in C language.
Operator Definition
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
The following are some examples of relational operators. c>=a+b , x<5.3 , n= =20 , count!=10

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 is an expression. If we need to build more than one condition for some
action to take place in programming, then we have to form compound condition.
Types of Logical Operators
Three types of Logical operators are available in C language.
Operator Definition
&& AND
|| OR
! NOT

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 5


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
a) 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 operator. If both of the relational expressions (conditions) are true then the compound
condition is considered true otherwise it is false.
Syntax: Expression1 && Expression2
Truth table for AND operator (*)
Expression-1 Expression-2 Expression-1 && Expression -2
True True True
True False False
False True False
False False False
Example: (a>=1) && (a<=10)

b) Logical OR (||) Operator Just like the logical AND operator, one relational expression is to the
left 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 OR operator (+)
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)

c) 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 of NOT operator: (-)
Expression !Expression
True False
False True
Example: !(a<b)
iii. Conditional Operators
A conditional (Ternary) 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
expression2. The entire conditional expression takes on a value and can therefore be used in an assignment
statement.
Example
a= (k>15)? x*y : x+y;

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 6


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Q5. Write a program that reads three numbers and prints their sum, product and average.

# include <stdio.h>
#include <conio.h>
void main(void)
{
int num1, num2, num3, sum, prod, avg;
printf(“Enter the three numbers separated by ‘,’\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\n”, sum);
printf(“the prod of the three numbers is: %d\n”, prod);
printf(“the avg of the three numbers is: %d\n”, avg);
getch();
}
Q6. Write a program that reads the length and width of a rectangle and prints its area.

# include <stdio.h>
#include <conio.h>
void main(void)
{
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);
getch();
}
Q7. 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(void)
{
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\n”, volume);
getch();
}

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 7


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Q8. 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(void)
{
float fahren, celsius;
printf(“Enter the temperature in celsius\n”);
scanf(“%f”, &celsius);
fahren= (9.0/5.0) * celsius + 32;
printf(“ %.2fC is equal to %.2fF\n”, celsius, fahren);
getch();
}

Q9. 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>
void main(void)
{
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);
getch();
}

--------- @@@ ENCORE STAR COLLEGE & ACADEMY @@@ ---------

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 8


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
CH-4: CONDITIONAL CONTROL STRUCTURE
Short Questions
Q2. Give short answers to the following questions.
i. Differentiate between if and if-else selection structures.
if Selection Structure if-else Selection Structure
If the condition is true then the block of statements The if-else statement is used in situation where
within the braces will be executed. some code is to be executed if a condition is true
and some other code is to be executed if the
condition is false.
If the condition is false then the block of statements If the condition is false then the block of
within the braces will be skipped and the control will statements following if will be executed and the
be transferred to the next statement of any exists. block of statements following else will be skipped.
General form: General form:
if (condition) if (condition)
{ {
Block of statements Block of statements
} }
else
{
Block of statements
}
ii. Differentiate between else-if and switch selection structures.
Else-if Selection Structure switch Selection Structure
The else-if is a type of conditional statement that The switch statement is similar to the else-if statement.
combines more than two conditions.
It allows the programmer to make a decision based on It is used when multiple choices are given and one
several conditions. choice is to be selected.
General form: General form:
if (condition-1) Switch (expression)
{ {
Block of statements case const-1:
} Statements;
else if(condition-2) break;
{ case const-2:
Block of statements statements;
} break;
.
else if(condition-3) .
.
{
default:
Block of statements
statements;
}
}
.
.
.
else
{
Block of statements to be executed
When none of the conditions is true.

PREPARED BY: ENCORE STAR |SCHOOL | COLLEGE | ACADEMY 0306-4941878 Page 1


030
ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
}
iii. What is nested selection structure?
The selection structure that is within another selection structure is known as nested selection structure.
Sometimes, in computer programming, it is required to use a selection structure within another selection
structure. In C language, the programmer can have a selection structure (if, if-else, else-if or switch
statement) within another selection structure.
iv. Write the following statement using if-else statement.
k=(a+b>20)? a+3*b: a-b;
Ans:
if (a+b)>20
k=a+3*b;
else
k=a-b;
v. Write the following statement using conditional operator.
if (x>y)
Z=(x+y)/3;
else
Z=x-5*y;
Ans: z=(x>y) ? (x+y)/3 : x-5*y;
vi. What will be the output of the following code?
int n, count, sum;
n=28; count=15; sum=30;
if (n<25)
{ count=count+5;
print(“\nCount=%d” ,count); }
else
{ count=count-5;
sum=sum+n;
printf(“\nCount=%d” , count);
printf(“\nSum=%d”, sum); }
Ans: Count=10
Sum=58
vii. What will be the output of the following code?
char ch;
ch=’c’;
switch(ch)
{ case ‘a’:
printf(“\n Good Morning! “); break;
case ‘b’:
printf(“\n Have a nice day! “); break;
case ‘c’: Ans: Good Bye!
case ‘d’:
case ‘e’:

PREPARED BY: ENCORE STAR |SCHOOL | COLLEGE | ACADEMY 0306-4941878 Page 2


030
ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
printf(“\n Good Bye “); break;
}

Q3. What is control structure? Explain conditional control structure with examples.
Control Structure:
In a programming language, a control statement is an instruction which determines the sequence of
execution of other statements in a program. Control structures are used in programs to implement
decisions.
Conditional control structure:
A conditional statement is an instruction in a programming language that contains a condition. When a
conditional statement is executed, first the condition is evaluated and then based on the result (true or
false), a particular statement or a set of statements is executed.
Conditional statements of C language are if, if-else, else-if and switch statements.
Structure of If Statement: Structure of If–else Statement:
General form: General form:
if (condition) if (condition)
{ {
Block of statements Block of statements
} }
else
{
Block of statements
}

Structure of else-If Statement: Structure of switch Statement:


General form: General form:
if (condition-1) switch (expression)
{ {
Block of statements case const-1:
} Statements;
else if (condition-2) break;
{ case const-2:
Block of statements statements;
} break;
else if (condition-3) .
{ .
Block of statements .
} default:
. statements;
. }
.
else
{

PREPARED BY: ENCORE STAR |SCHOOL | COLLEGE | ACADEMY 0306-4941878 Page 3


030
ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Block of statements to be executed
When none of the conditions is true.
}
Q4. What is the purpose of switch () statement? Explain with the help of one example.
General form:
switch(expression)
{
case const-1:
Statements;
break;
case const-2:
statements;
break;
.
.
.
default:
statements;
}

Purpose of switch statement:

• The switch statement is similar to the else-if statement. It is used when multiple choices are
given and one choice is to be selected.
• When switch statement is executed, the expression is evaluated. Based on the result of
expression one of the cases in the switch statement is executed. The result of expression is
compared with the constant values given after the keywords case. If the result matches the
constant value after any case then the statements under that case are executed.
• In switch statement, it is allowed to use a variable within the parenthesis instead of an
expression based on which statements under a case can be executed.
• The purpose of break statement is to exit the body of the switch statement after executing the
statements under a case and transfer control to the first statement following the end of the
switch statement.
• If no case is matched then the statements under the default keyword are executed. Its use is
optional. If it is not used then the controls exits from the body of the switch statement and goes
to the first statement following the end of the switch statement.
• The expression should be of type int, char but not float.

Example of switch statement

PREPARED BY: ENCORE STAR |SCHOOL | COLLEGE | ACADEMY 0306-4941878 Page 4


030
ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
#include<stdio.h>
#include<conio.h>
void main (void)
{
int n;
printf(“\nEnter an integer, N: “);
scanf(“%d”, &n);
switch (n)
{
case 1:
printf (“N is 1”);
break;
case 2:
printf (“N is 2”);
break;
default:
printf (“N is not 1 or 2”);
}
getch();
}

PREPARED BY: ENCORE STAR |SCHOOL | COLLEGE | ACADEMY 0306-4941878 Page 5


030
COMPUTER SCIENCE NOTES (10TH CLASS) CHAPTER-5

CH-5: LOOP CONTROL STRUCTURE

Q2. Give short answers to the following questions.


i. Differentiate between for loop and while loop.
For loop While loop
The for statement is used to executed a set of The while statement is used to implement
statements repeatedly for a fixed number of repetition structure in a program when the
times in a program. number of iterations is not known in advance and
the repetition continues until some condition
remains true.

ii. Differentiate between while loop and do while loop.


While() loop Do-while() loop
1 While() loop is pre-tested loop. Do-While is post-tested loop.
2 The Syntax or general form of while() loop The Syntax or general form of do-while() loop
While(condition) do{
{ {
Statements; //body of loop Statements; //body of loop
} } while (condition)
3 In ‘while’ loop the controlling condition In ‘do-while’ loop the controlling condition
appears at the start of the loop. appears at the end of the loop.
4 The iterations do not occur ,if the condition The iterations occurs at least once even if, the
at the first iteration, appears false. condition is false at at the first iteration.

iii. What will be the output of the following code?


int k;
for(k=1; k<=5; k++);
printf(“\nI am a student”);
printf(“\nGOOD BYE”);

Ans: I am a student
I am a student
I am a student
I am a student
I am a student
GOOD BYE

ENCORE STAR COLLEGE & ACADEMY ISLAMABAD 03064941878 Page 1


COMPUTER SCIENCE NOTES (10TH CLASS) CHAPTER-5

iv. What will be the output of the following code?


int n;
for(n=30; n>=10; n=n-5)
printf(“\n%d”,n);

Ans:
30
25
20
15
10
v. Find errors in the following code.
int k, a
a=3;
k=1;
while(k<10);
{
Printf(“\n%f\t%f”, k, k*a-1);
K=k+2;
}
Ans:
int a, k;
a=3;
k=1;
while(k<10)
{
printf(“\n%d\t%d”, k, k*a-1);
k=k+2;
}
Output:
1 2
3 8
5 14
7 20
9 26
vi. Convert the following for loop into a while loop.
Int k; Ans: Int k;
for(k=25; k>0; k=k-3); k=25;
Printf(“\n%d”, k); while(k>0);
{
Printf(“\n%d”, k);
K=k-3;
}

ENCORE STAR COLLEGE & ACADEMY ISLAMABAD 03064941878 Page 2


COMPUTER SCIENCE NOTES (10TH CLASS) CHAPTER-5

Q3. What is looping structure? Explain for loop with examples.


A loop is a structure that enables the programmer to execute the same sequence of statements repeatedly until a
particular condition is met.
For loop / For statement:
The for is a looping statement which is used to execute a set of statements repeatedly for a fixed number of
times. It is also known as counter loop. It has the general form:
for (initialization; test condition; increment/ decrement)
{
body of the loop
}
When for statement is executed, a variable (also known as loop variable)is assigned an initial value in the
initialization part of the loop, such as k=1 or count=0.
After the execution of the body of the loop, the control is transferred back to the increment/decrement part of
the loop. Body of the loop may have one or more statements. If it contains only a single statement then braces
are not needed.
Note: all the three expressions such as initialization, test condition and increment are optional. You can omit any
or all in for statement.
Examples:
for(; ;)
for(int i=1 ; ;)
for(; k<10; k++)
for(; ; k++)
for(; x<12 ;)

Q4. Explain while and do-while loops with examples.


The while statement is used to implement repetition structure in a program when the number of
iterations is not known in advance and the repetition continues until some condition remains true.
While() loop is pre-tested loop.

The Syntax or general form of while() loop


While(test condition)
{
Statements; //body of loop
}
Example: Program prints all the upper-case letters on a single line using while loop. The ASCII code for upper-case letters
are in the range 65 to 90.
#include<stdio.h>
#include<conio.h>
Void main(void)
{
int n;

ENCORE STAR COLLEGE & ACADEMY ISLAMABAD 03064941878 Page 3


COMPUTER SCIENCE NOTES (10TH CLASS) CHAPTER-5

printf(“\n”);
n=65;
while(n<=90);
{ OUTPUT
Printf(“%c”, n);
ABCDEFGHIJKLMNOPQRSTUVWXYZ
n=n+1;
}
getch();
}

The do while statement is used to implement loop structure when it is required to execute the loop at least one.
The Syntax or general form of do while() loop
do
{
body of loop
}
While (test condition);
Example: Program prints all the lower-case letters on a single line using do while loop. The ASCII code for lower-
case letters are in the range 97 to 122.
#include<stdio.h>
#include<conio.h>
Void main(void)
{
int n;
printf(“\n”);
n=97;
do
{ OUTPUT
Printf(“%c”, n);
n=n+1; abcdefghijklmnopqrstuvwxyz
}
while(n<=122);
getch();
}

Q5. Explain the purpose of break and continue statements with one example each.
The Break statement:
C language provides the break statement to exit from a loop as soon as certain condition occurs. It is used in for,
while and do while statements. Break statement is also used to exit the body of switch statement after
executing the statements under a case and transfers control to the first statement following the end of the
switch statement.
Example: Program to check for prime number by using break statement.

ENCORE STAR COLLEGE & ACADEMY ISLAMABAD 03064941878 Page 4


COMPUTER SCIENCE NOTES (10TH CLASS) CHAPTER-5

#include<stdio.h>
#include<conio.h>
void main(void)
{
int num, k, a=0;
printf(“\nEnter a number:”);
scanf(“%d”, &num);
for(k=2; k<=num/2; k++)
{
OUTPUT
a=1;
Enter a number: 23
break;
23 is a prime number
}
If(a==0)
printf(“\n%d is a prime number.”, num);
else
printf(“\n%d is not a prime number.”, num);
getch();
}

The Continue statement:


Sometimes during the execution of a loop, when a certain condition occurs after executing a statement, it may be required
to skip the remaining statements within the body of the loop and continue loop for next iteration until the test condition in
met. C language provides the continue statement to achieve this task. The continue statement causes the loop to be
continued with the next iteration after skipping the remaining statements within the body of the loop.
Example: Program to read 10 numbers and find the sum of those that are less than 20 by using continue
statement.
#include<stdio.h>
#include<conio.h> Output
Void main(void) Enter an integer: 12
{ Enter an integer: 5
int sum=0, k, a; Enter an integer: 56
for(k=1; k<=10; k++) Enter an integer: 15
{ Enter an integer: 65
printf(“\nEnter an integer:”); Enter an integer: 40
scanf(“%d”, &a); Enter an integer: 2
if(a>=20) Enter an integer: 34
continue; Enter an integer: 9
sum=sum+a; Enter an integer: 17
} Sum of numbers less than 20 is 60
printf(“\nSum of numbers less than 20 is %d ”, sum);
getch();
}

ENCORE STAR COLLEGE & ACADEMY ISLAMABAD 03064941878 Page 5


COMPUTER SCIENCE NOTES (10TH CLASS) CHAPTER-5

Q6. What is nested loop? Give two examples.


In C language, it is allowed to nest loops within another loop. Nested loops can be of any kind. For
example, the programmer can nest a for loop inside a while loop or inside a do while loop. Loops can be
mixed in any way as required.

Example-1: Program by using nested loop to print the products of numbers as given below.
1x1=1 Output
Program
1x2=2 1x1=1
#include<stdio.h>
1x3=3 1x2=2
1x4=4 #include<conio.h>
1x3=3
2x1=2 void main(void) 1x4=4
2x2=4 { 2x1=2
2x3=6 int j, k, prod; 2x2=4
2x4=8 for(j=1; j<=3; j++); 2x3=6
3x1=3 for(k=1; k<=4; k++); 2x4=8
3x2=6 { 3x1=3
3x3=9 3x2=6
Prod=j*k;
3x4=12 3x3=9
printf(“\n%2d x %2d = %2d”, j, k, prod);
3x4=12
getch();
}
1x2=2

Example-2: Program by using nested loop to calculate the sum of the integers for each integer
1x1=1 from 1 to n,
where n is the value that the user of the program enters. 1x2=2
1x3=3
#include<stdio.h> 1x4=4
#include<conio.h>
void main(void) OUTPUT 1x1=1
{ 1x2=2
int j, k, sum, n; Enter the upper limit (n): 8 1x3=3
1x4=4
printf(“\nEnter the upper limit (n) :”);
Integer Sum 2x1=2
scanf(“%d”, &n);
2x2=4
printf(“\n\tInteger\t\tSum”); 1 1 2x3=6
for(j=1; j<=n; j++) 2 3 2x4=8
{ 3 6 3x1=3
Sum=0; 4 10 3x2=6
for(k=1; k<=j; k++) 5 15 3x3=9
sum=sum+k; 6 21 3x4=12
7 28
printf(“\n\t%3d\t\t %3d, j, sum);
8 36
}
getch();
}

ENCORE STAR COLLEGE & ACADEMY ISLAMABAD 03064941878 Page 6


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
CH-6: COMPUTER LOGIC AND GATES

Q2. Give short answers to the following questions.


i. What is a logic gate?
Logic gates are the basic building blocks of digital computer. Logic gates operate on two voltage levels and
process digital signals which represent binary digits 0 and 1.

ii. Define truth table.


A truth table represents a digital logic circuit in table form. It shows how a logic circuits output responds to all
the possible combinations of the inputs using logic ‘1’for true and logic ‘0’ for false.

iii. Define Boolean function.


A Boolean function can be transformed from an algebraic expression into a logic circuit composed of
AND, OR and NOT gate.
A Boolean function is an expression formed with binary variables, the logical operators (OR, AND
and NOT), parenthesis and equal sign. A binary variable can take the value of 0 and 1. Ex F=x+y

iv. What is Karnaugh map and why is it used?


Karnaugh Map (k-map) is a pictorial form of a truth table. It consists of square boxes called cells. All
the possible combinations of variables involved in a Boolean function are written inside the cells in
their respective positions. A two –variable K-map contains 22=4 cells, three-variable 23=8 cells and
so forth.
Why is it used? K-map is used for logic simplification. A Karnaugh map provides a pictorial method
of grouping together expressions with common factors and therefore eliminating unwanted
variables.
v. Draw three-variable Karnaugh map for variable X, Y and Z.

𝑿 𝑿 X X

̅𝒀
𝑿 ̅𝒁
̅ ̅𝒀
𝑿 ̅𝒁 ̅
𝑿𝒀𝒁 ̅ 𝒀𝒁
𝑿 ̅
𝑿

̅𝒁
𝑿𝒀 ̅ ̅𝒁
𝑿𝒀 XYZ ̅
𝑿𝒀𝒁
X

𝒁 Z Z 𝒁

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 1


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878

Q3. Draw the graphical symbols of AND, OR, NOT, NAND and NOR gates and write their functions.
There are three basic logic gates used in digital circuit s which are AND, OR and NOT gates.
AND GATE
▪ The AND gate has two or more inputs that can be LOW (0) or HIGH (1).
▪ The output is HIGH only when all the inputs are HIGH.
▪ It produces a LOW output when at least one of the inputs is LOW.
The logic operation of a two-input AND gate is shown below with all the possible input combinations
and the resulting output for each.

OR GATE
▪ The OR gate has two or more inputs.
▪ The output of an OR gate is LOW only when all the inputs are LOW.
▪ It produces a HIGH output when at least one or more of its inputs are HIGH.
The logic operation of a two-input OR gate is shown below with all the possible input combinations
and the resulting output for each.

NOT GATE
▪ The NOT gate performs the functions of inversion.
▪ Therefore, it is also known as inverter. It has a single input.
▪ The output of a NOT gate is HIGH when the input is LOW and vice versa.
The logic operation of a NOT gate is shown below with LOW and HIGH inputs.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 2


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
NAND GATE

▪ The NAND gate combines the AND and NOT gates.


▪ Such that the output will be 0 only when all the inputs are 1 as shown in Fig.
▪ Its logic expression is F=xy which indicates that inputs x and y are first ANDed and then the result is
inverted.
▪ Inversion is indicated by a bar.
▪ Thus, an AND gate always produces an output that is the inverse(opposite) of an AND gate.
Symbol, expression and truth table of NAND gate is shown given below

NOR GATE
▪ The NOR gate combines the OR and NOT gates.
▪ Such that the output will be 0 when any input is 1 as shown in Fig.
▪ Its logic expression is F=x + y which indicates that inputs x and y are first ORed and then the result is
inverted.
▪ Inversion is indicated by a bar.
▪ A NOR gate always gives an output that is the inverse(opposite) of an OR gate.
Symbol, expression and truth table of NOR gate is shown given below

Q4. Explain how NAND and NOR gates can be created using AND, OR and NOT gates.
i. The NAND gate can be easily created by using an AND gate and a NOT gate as shown in Fig.

ii. The NOR gate can be easily created in a similar way by using an OR gate and a NOT gate as shown in Fig.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 3


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Q5. Draw truth table of the following Boolean functions.
(i) 𝐹 = 𝑥̅ 𝑦𝑧̅ + 𝑥̅ 𝑦𝑧 + 𝑥𝑦𝑧̅
x y z ̅
𝒙 ̅
𝒚 𝒛̅ ̅𝒚𝒛̅
𝒙 ̅𝒚𝒛
𝒙 𝒙𝒚𝒛̅ F
0 0 0 1 1 1 0 0 0 0
0 0 1 1 1 0 0 0 0 0
0 1 0 1 0 1 1 0 0 1
0 1 1 1 0 0 0 1 0 1
1 0 0 0 1 1 0 0 0 0
1 0 1 0 1 0 0 0 0 0
1 1 0 0 0 1 0 0 1 1
1 1 1 0 0 0 0 0 0 0

(ii) 𝐹 = 𝑥̅ 𝑧 + 𝑦𝑧̅ + 𝑥𝑦𝑧


x Y z ̅
𝒙 ̅
𝒚 𝒛̅ ̅𝒛
𝒙 𝒚𝒛̅ 𝒙𝒚𝒛 F
0 0 0 1 1 1 0 0 0 0
0 0 1 1 1 0 1 0 0 1
0 1 0 1 0 1 0 1 0 1
0 1 1 1 0 0 1 0 0 1
1 0 0 0 1 1 0 0 0 0
1 0 1 0 1 0 0 0 0 0
1 1 0 0 0 1 0 1 0 1
1 1 1 0 0 0 0 0 1 1

(iii)𝐹 = 𝑥̅ 𝑦̅𝑧̅ + 𝑥̅ 𝑦̅𝑧 + 𝑥̅ 𝑦𝑧 + 𝑥𝑦𝑧̅


x Y z ̅
𝒙 ̅
𝒚 𝒛̅ ̅𝒚
𝒙 ̅𝒛̅ ̅𝒚
𝒙 ̅𝒛 ̅𝒚𝒛
𝒙 𝒙𝒚𝒛̅ F
0 0 0 1 1 1 1 0 0 0 1
0 0 1 1 1 0 0 1 0 0 1
0 1 0 1 0 1 0 0 0 0 0
0 1 1 1 0 0 0 0 1 0 1
1 0 0 0 1 1 0 0 0 0 0
1 0 1 0 1 0 0 0 0 0 0
1 1 0 0 0 1 0 0 0 1 1
1 1 1 0 0 0 0 0 0 0 0

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 4


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
(iv) 𝐹 = 𝑥𝑧̅ + 𝑥̅ 𝑦̅
x y z ̅
𝒙 ̅
𝒚 𝒛̅ 𝒙𝒛̅ ̅𝒚
𝒙 ̅ F
0 0 0 1 1 1 0 1 1
0 0 1 1 1 0 0 1 1
0 1 0 1 0 1 0 0 0
0 1 1 1 0 0 0 0 0
1 0 0 0 1 1 1 0 1
1 0 1 0 1 0 0 0 0
1 1 0 0 0 1 1 0 1
1 1 1 0 0 0 0 0 0

Q6. Simplify the Boolean functions of Question 5 using Karnaugh map.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 5


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878

Q7. Draw the logic circuits of the following Boolean functions.


(i) 𝐹 = 𝑥̅ 𝑦̅𝑧 + 𝑥𝑦̅𝑧
(ii) 𝐹 = 𝑥̅ + 𝑦𝑧
(iii) 𝐹 = 𝑥𝑦̅ + 𝑥𝑦̅𝑧 + 𝑥𝑦𝑧
(iv) 𝐹 = 𝑥 + 𝑦̅ + 𝑦𝑧̅

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 6


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 7


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
CH-7: WORLDWIDE WEB AND HTML

Q2. Give short answers to the following questions.


i. Differentiate between website and Web server.
Website:
• A website is a collection of related Web pages hosted on a Web server.
• It is accessible through an internet address known as Uniform Resource Locator (URL).
• A URL is what the user types in Web browser such as Internet Explorer to display a particular website.
• Example a website for air ticket reservation & website of FBISE.
Web Server:
• A Web server is a computer that makes Web pages available through the internet.
• Web servers are used to host websites, data storage and also for running applications.
• Web servers deliver HTML documents when an Internet user types a URL of a website in the browser.
• Any computer can be turn into a Web server by installing server software and connecting the computer to the
internet.
ii. Describe how a search engine is used for searching information on the Internet.
Search Engine: A search engine is a website or software that allows people to find information on the WWW.
How to use Search Engine:
Search engines have been designed to help users find specific information on the web.
• Users type in one or more keywords in a search engine.
• The search engine will look for matching websites from all over the web.
• User will see the total number of matches found and then the first ten sites that most closely match the
keywords.
• The information shown for each website includes a title and a brief description.
• To display the next ten sites, the user clicks a button labeled Next Results, Next Page or a similar button at the
bottom of the page.
• If the user wants to open any site, he just clicks on the hyperlink.
Example: Google and Yahoo etc.

iii. Define URL and Web hosting.


URL: Its stands for Uniform Resource Locator. It is the address of a file or resource on the Web. When the user
types the URL of a website in the browser it retrieves the Web pages of that site. It consists of three parts.
• Protocol: The first part, http:// is known as protocol. It tells the server what type of file is being requested.
• Resource name: It is the address of a specific computer where the website is located. It consists of three parts,
separated with dots (.) between them.
o Domain name: Name of the website. This is the name that the browser uses to check with the web
server whether this site exits or not.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 1


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
o Top-level domain: The .com is referred to as the Top Level Domain (TLD). This is the name the
browser will use to resolve the location of the requested site. Most common TLDs are .com, .org, .net
etc.
Some sites have another part next to the top level domain for example .pk (for Pakistan), .uk, us etc.
This is called country-code top-level domain (ccTLD). They are specific to each country.
• File path: it links to a specific page or resource in the website.

Protocol Domain name Top-level domain

Example: http:\\www.aiou.edu.pk/NewStudents.asp

Sub-domain File Path

Web Hosting:
• Web hosting is a service that uploads a website on a Web server and makes it available for computer users.
• It makes the website running on a computer all the time that is connected to Internet.
• To host a website a Web server and a permanent connection to the Internet is required.
• It also requires website administration such as managing and configuring a Web server and virus protection.
• Computer users’ first design Web pages on their computer and test it to ensure they work properly.
• When Web pages are ready they are uploaded on a Web server to make them available for others.
• There are companies that provide Web hosting service.
• These companies provide 24 hour support and online control for managing the website.

iv. Describe HTML.


• HTML stands for Hyper Text Markup Language.
• HTML is a mark-up language used to create Web pages.
• Language of Internet’s World Wide Web (WWW).
• Used to create hypertext documents that bring together text, pictures, sounds, video clips and links all in one place.
• A browser such as Internet Explorer or Google Chrome is used to read HTML document and display it on the
screen as web pages.
• HTML files are plain text files, so they can be created using a simple editor such as Notepad or Wordpad.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 2


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878

Q3. Describe any four types of websites.


1. Web Portals:
Web portal is a website that offers a large variety of services. These include online shopping malls, news, stock
prices, e-mail, search engines, etc. Web portals function as a point of access to information on the Web.
Example: a school Web portal delivers information about the school’s historical background, admission
requirement, school curriculum, tuition fees structure, school news, student results, etc.
Like Web portal of NUST is www.nust.edu.pk
2. News Websites:
News websites provide information about current events and opinions. These sites publish news stories and let
their visitors’ voice be heard. These sites provide their visitors a way to get their thoughts and views published.
Therefore, a link for the readers comment is provided for feedback.
Examples: www.thenews.com.pk , www.nation.com.pk etc.
3. Educational Websites:
Educational websites are created for educational purpose. These sites contain animation, slide presentations
and tutorials to educate people on various topics. Information is presented in a very well organized way. The
purpose of any educational website is to impart knowledge to people who are looking for better understanding
of a topic and pursue knowledge.
Examples: www.sabaq.pk., www.fbise.edu.pk
4. Entertainment Websites:
A website that provides some form of entertainment to computer users is known as entertainment website.
Entertainment websites include sports, music, movies, radio, television, computer game, comedy, fashion and
theater websites. These websites are created by those people who find enjoyment in providing information
about people to visitors that they never know before and to reach people who have common interests.
Examples: www.youtube.com, www.facebook.com etc.
5. Business Websites:
Business websites provide facilities to maintain business relationships and selling information, services and
commodities via internet. These sites facilitate exchange of business documents, such as orders or invoices
between suppliers and customers.
Examples: banking, ticket reservations and stock market transactions. Like www.hbl.com, www.pia.com etc.
Q4. Write the HTML tags for the following?
i) Paragraph:
<p></p>
For example: <p>Tags are used to define paragraph.HTML automatically adds an extra blank line before and after
a paragraph. Tags are used to define paragraph……………………...………………………………………………………………………</p>
Result: Tags are used to define paragraph.HTML automatically adds an extra blank line before and after
a paragraph. Tags are used to define paragraph……………………...………………………………………………………………………
ii) Heading:
There are six heading tags, <h1></h1> to <h6></h6>. The h1 tag is used for largest heading and h6 tag used for smallest.
<h1></h1>
For example: <h1>COMPUTER</h1>

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 3


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Result: COMPUTER
iii) Bold:
<b></b>
For example: <b>computer</b>
Result: computer
iv) Underline:
<u></u>
For example: <u> computer</u>
Result: computer
v) Italic:
<i></i>
For example: <i> computer</i>
Result: computer
vi) Center text:
<center></center>
For example: <center> computer</center>
Result: computer
vii) Strike out:
<strike></strike>
For example: <strike> computer</strike>
Result: computer
viii) Superscript:
<sup></sup>
For example: X<sup>Y</sup>
Result: XY
ix) Subscript
<sub></sub>
For example: X<sub>Y</sub>
Result: XY
x) Font size:
These tags are used to change the font size. Replace the ? symbol with a number in the range 1 to 7. 1 is the smallest and 7 is
the largest font size.
<font size=?></font>
For example: <font size=3>computer</font>
Result: computer
xi) Font color:
These tags are used to change the font color. Replace the ? symbol with color such as black, brown, gray, green, red, yellow
etc.
<font color=?></font>
For example: <font color=black>computer</font>
Result: computer
xii) Font typeface:
These tags are used to change the font face of text that is within the tags. Replace the ? symbol with font face such as arial,
times new roman, calibri etc.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 4


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
<font face></face>
For example: <font face>computer</font>
Result: computer
Q5. Create a Web page in HTML that displays images of a computer. The width of image should be 350 pixels
and height 220 pixels.

Q6. Describe how background color and image are applied to Web page.
Applying Background and Foreground colors
To apply a background color in a page, insert the bgcolor attribute and for foreground color, insert text attribute in the
<body> tag.

Applying Background Image


An image can be set as background of a page using the background attribute in the <body> tag
<body background=”image.jpg”>

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 5


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Q7. Create an HTML document that contains a graphical hyperlink.
The syntax of creating a hyperlink to another web page is:
<a href= “url”>text to be displayed</a>
For example:
<a href= “http://www.fbise.edu.pk”>Visit Federal Board</a>
It will be displayed as Visit Federal Board in the browser window and when the reader clicks on it, it will open the Federal
Board website that has the URL www.fbise.edu.pk
The HTML document in fig below demonstrates the use of hyperlinks in a web page.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 6


ENCORE STAR COLLEGE & ACADEMY
+92-306-4941878
Q8. Describe the tags used for creating a table in HTML.
Tables are very often used in Web pages to present information in an organized manner which is easy to read and
understand by the user.
A table consists of rows and columns.
<table></table>: These tags are used to create a table.
<tr></tr>: These tags define a table row.
<td></td>: These tags define data cell. Column of each row is called data cell. A data cell can contain text, image,
paragraph, etc
The HTML document for creating the following table for date sheet of examination is shown in figure below. This table
has 5 rows and 4 columns.

PREPARED BY: ENCORE STAR COLLEGE & ACADEMY +92-306-4941878 Page 7

You might also like