Programming Techniques and Problem Solving
Programming Techniques and Problem Solving
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.2
carried out in a correct order. Design a plan of action chart including all the details you need
to consider to carry it out and when each phase should happen. After analysis the solution is
planned. There can be various solutions but the proposed solution should be cost-effective,
time efficient, speedy and less complex.
Question: Define and explain the problem solving techniques. Q#.02 at exercise
The following are the some useful techniques:
Abstraction: Solving the problem in a model of the system before applying it to real system.
Explanation: Create a model before the solution of a problem. For example if we want to
construct a building first of all we will develop a model and draw a map for it. Then we will
construct that building accordingly.
Analogy: Using a solution that solved an analogies (similar) problem.
Explanation: Some time more than one problem has same nature of solution so in solving
problems we must solve our problems in such a way that solve same problems
Brainstorming (suggesting): (especially among groups of people) suggesting a large
number of solutions and ideas combining and developing then until best solution is found.
Explanation: A problem may have more than one solution so combine all the suggested
solution and then find the best solution in them.
Divide and conquer (get solution): Breaking down a large, complex problem into smaller
solvable problem.
Explanation: Commonly a complex problem is solved by “Divide and Rule” method. So a
large or complex problem is solved by breaking it in to sub problems. As the solution of sub
problems is easy. Then solution of problems must be in such a way that leads to the solution
of the whole problem.
Reduction: Transforming the problem into another problem for which solution is exist.
Explanation: In this sense the problem represents the class, since any solution to it can, in
combination with the reductions, be used to solve every problem in the class.
Research: using existing ideas or adopting existing solutions to similar problems.
Explanation: when same problem come repeatedly then adopt the exist solution for it.
Trial and error: Testing possible solutions until the right one is found.
Explanation: A problem may have a number of solutions so test each solution and find the
best one. In this technique each suggested solutions are solved practically then the best
solution is selected.
Q. What is meant by Candid solution of a problem?
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.3
Ans: Definition: Finding the best and suitable solution of any problem is known as candid
solution.
Explanation:
Solution is the act or process of solving a problem as to obtain the desired and best result
with minimum cost. There may be several solutions for solving the problem. However
finding “the best” and “the most suited” solution is the candid solution of the problem.
Question: Describe the factors which are needed for the selecting of best solution.
Ans: there are some factors which are very important in selecting the best solution of any
problem given as below:
1. Speed: Speed is an important factor in selecting the best and suitable solution of a
problem. The best solution toward a problem is one which takes minimum time to
solve.
2. Cost: Cost is another factor for selecting the best solution of any problem. Cost of
solution includes space and other hardware, software requirements. The solution will
be considered to be the best if it gives optimal desired result with minimum cost.
3. Complexity: complexity means the resources required to the solution, most
commonly known as the time require which a problem take to solve, also the steps as
in how many steps a problem will solve and the space which is needed for the solution
of a problem as how much memory it will take.
Q#.03 at exercise
Define an algorithm explain the role of algorithm in problem solving.
Algorithm:
Def.#01: It is well defined list of steps for solving a particular problem
Def.#02: A step by step solution toward a problem.
Def#.03 It is a number of steps in which a problem is solved.
Explanation: An algorithm is number of steps in which a problem is to be solved. A
problem can only be solved if the operations for its solution are carried out in a correct order.
So the order of steps must be well defined. An algorithm is sequence of computational steps
that takes some values as input and produces some values or set of values as output. We can
say that algorithm as a tool for solving well-specified computational problems.
Role of algorithm in problem solving:
Algorithm plays an important role in developing solution to a problem, for example if we
have a complex situation which requires a lot of information to be gathered and a lot of data
to be processed. Then by dividing each activity of this information gathering and data
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.4
processing into steps then it will make the problem solving an easy task. As the “Divide and
Rule “method make the problem solving is an easy task.
Example: If we want to find the average of 20 numbers, if this process is defined in small
discrete steps then it make the solution very easy.
Step-1 Select any 20 numbers
Step-2 add all the numbers
Step-3 Divide the result of step-2 by total no of observations i.e 20
Step-4 the average will be calculated.
To determine which algorithm is "best". There are generally two factors used to determine
whether one algorithm is "better" than another.
Space requirements (i.e. how much memory is needed to complete the task).
Time requirements (i.e. how much time will it take to complete the task).
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.5
Algorithm: Given five number n1,n2,n3,n4 and n5. This algorithm finds the sum SUM,
product PRO and average AVG of these numbers
Step#01 Read n1,n2,n3,n4,n5
Step#.02 Set Sum= n1+n2+n3+n4+n5
Step#.03 Set PRO= n1*n2*n3*n4*n5
Step#.04 Set AVG=SUM/5
Step#.05 write SUM, PRO, AVG
Step#.06 Exit
Problem#.02 To display largest one out of the three given unequal numbers.
Algorithm: Given three unequal numbers n1,n2 and n3. This algorithm finds the largest
LARG number out of three numbers.
Step#.01 Read n1,n2,n3
Step#.02 IF n1>n2 AND n1>n3 then set LARG=n1 Else if n2>n3 then set LARG=n2
ELSE set Large=n3
Step#.03 Write LARG
Step#.04 Exit
Problem#03
To find acceleration of a moving object with given mass and the force applied.
Step#.01 Read Mass,Force
Step#.02 Set Acc=Force/Mass
Step#.03 Write acc
Step#.04 Exit
Problem #05: To find the area of a triangle
Step#.01 READ B,H
Step#.02 Set Area=B*H/2
Step#.03 Write Area
Step#.04 Exit
Problem#06 To find the area of a parallelogram
Algorithm :this algorithm find the area of a parallelogram AP having base B and height H.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.6
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.7
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.8
Step#.08 Exit
Problem#.13 To produce a multiplication table TAB for a given number.
Algorithm. This algorithm produce a multiplication table TAB for a given number NUM.
Step#.01 Set C=1 (initialize counter)
Step.02 READ NUM
STEP.03 Repeat Step 4, 5 and 6 while C<=10
Step#.04 Set TAB=NUM*C
Step#.05 Write NUM’*’C’=’TAB
Step#.06 C=C+1
End of while loop
Step#.07 Exit
Problem#.14 To convert Celsius to Fahrenheit temperature
Algorithm: This algorithm converts Celsius C to Fahrenheit F temperature.
Step#.01 READ C
Step#.02 Set F=32+C*9/5
Step#.03 Write F
Step#.04 Exit
Problem#15 To convert Fahrenheit to Celsius Temperature
Algorithm: this algorithm converts Fahrenheit F to Celsius C temperature
Step#.01 READ F
Step#.02 Set C=(F-32)*5/9
Step#.03 Write C
Step#.04 Exit
Problem#.16 Find even number in integers ranging from n1 to n2 . (where n1 is less than n2
and n1 is an even number)
Step#.01 READ N1 N2
Step#.02 Set EVEN=n1
Step#.03 Repeat Step 4 and 5 while EVEN<=n2
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.9
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.10
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.11
3. Process Box:
A rectangle is used for representing a process. It shows the process or calculation in
the program flow chart.
4. Decision Box:
6. Annotation symbol:
This symbol is used to represent remarks given by the programmer to a particular
part/ Component of the flow chart.
………………..
7. On page connector:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.12
This symbol is used to connect a part of the flow chart to another when flow chart is
divided the in to many parts at same page. Circle is used to represent on page
connector.
Q: What are the Flow chart requirements when the solution of a problem is to be designed,
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.13
EXERCISE
Q#.01Select the most appropriate choice;
i. An obstacle, difficulty or challenge or any other situation that require resolution is
called_____________.
a. Algorithm b. Problem c. Complexity d. None of the above
ii.___________ is used to describe properties of an algorithm relating to how much
various types of resources in consumes.
a. Efficiency of an algorithm b. Candid solution
c. Complexity of an algorithm d. None of the above
iii. ____________is a well defined list of steps for solving a particular problem.
a. Algorithm b. Flowchart c. Complexity of an algorithm d. All of the above
iv. An algorithm is a sequence of computational steps that transform the input into
_____________.
a. Data structure b. algorithm c. Output d. all of the above
v. Which symbol is used to represent a process?
a. b. c. d.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.14
Q#.02 Give short answer to the following questions. For ANS See page#
i. Define Problem analysis
ii. How the solution of the problem is planned.
iii. Define Candid solution of a problem.
iv. Define any three problem solving techniques
v. List various factors for selecting the best solution of any problem.
Ans: The following factors are used for selecting the best solution of any problem.
speed
cost
complexity
Q#.03 Define an algorithm and explain the role of algorithm in problem solving
process.
See at page#
Q#.04 Describe the criteria for measuring efficiency of an algorithm of on the basis of:
a. Input needed
b. Processing to be completed
c. Decision to be taken
d. Outputs to be provided
For ANS See page#
Q#.05 what is flowchart? Explain flowchart symbols in detail.
For ANS See page#
Q#.06 Write an algorithm to calculate the factorial of given number and draw
flowchart for it.
Ans:
Algorithm:
Step#.01 Read NUM
Step#.02 Set Fac=1 and c=1
Step#.03 Set Fac=Fac*k
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.15
Set K=K+1
Step#.04 Repeat step#.03 if K<=N otherwise proceed to step#.05
Step#.05 Display Fac
Step#.06 Exit
Note: for flowchart see Problem#. 12 at page#30 in book.
Q#.07 Write an algorithm and draw a flowchart to resolve quadratic equation.
Algorithm:
Step#.01 Read a,b,c
Step#.02 Set X= -b+(b*b-4ac)^1/2
Step#.03 Set Y=2*a
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.16
Chapter#.02 PROGRAMMING IN C
Q#.02(ii) at exercise
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.18
Low-level languages
[Link]
Low-level languages are difficult to learn because its instruction contain binary code or
symbols.
[Link]
Low-level languages are far from human languages.
[Link]
Programs in low-level languages are fast in execution.
[Link]
Programs in low-level languages are difficult to modify.
[Link] at hardware level
Low-level languages provides facility to write programs at hardware level.
[Link] of hardware Deep
Deep knowledge of hardware is required to write programs.
[Link]
These languages are normally used to write hardware programs.
Q. What is high level language, Explain any five types of high level language. Q#.04 at
exercise
Ans: High Level Language:
General purpose language: Mostly High level languages are the general purpose languages.
It means that these languages are applicable in every field like education, health, research,
engineering etc.
English oriented language: High level languages are English oriented languages. Its
instruction contain commonly used English words like IF, FOR, WHILE, TO etc.
Easy to learn: As its instruction contains the English words so it is suitable for the person
who wants to learn programing.
Need Translator: High level language instruction is translated into machine language by
translating software. Today compilers are used as translator for programing language.
Examples of high level language: C,C++,JAVA, Visual Basic, Oracle etc:
(I). C Programing language:
Development: C is a programming language developed at AT & T’s Bell Laboratories of
USA in 1972. It was designed and written by a man named Dennis [Link].
Procedure Language: C is a procedure programing language with many low level features
including the ability to handle memory address and bits.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.19
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.20
Scripting language: PHP is an HTML embedded scripting language. The goal of the
language is to allow web developer to write dynamically generated web pages.
General purpose programming language: It is general purpose programming language.
But is generally used to create web pages.
No Compiler Interpreter
Memory
4 Requirement :More (Since Object Memory Requirement is Less
Code is Generated)
Errors are displayed after entire Errors are displayed for every instruction
6
program is checked interpreted (if any)
Question#.05 at exercise
Question: Explain different parts/modules of C language IDE?
Ans: Module of C Programming language: The following are the module of C
programming language:
1. Editor: It is a program which enables users to create and edit program in C language. The
written program is known as source code and having extension “.c”. Editor is also known as
text editor. Most of the editors automatically highlight compilation error.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.21
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.22
2 Source program is written in higher level Object programs usually contain lower
languages such as Java or C level languages such as Assembly
language or machine code.
3 Source programs are easily readable by Object programs are difficult to read by
humans. humans.
Question#02(ix) at exercise
Q: what is reserved word?
Ans: Reserved word/Keyword:
Def#.01: The word that has specific meaning to the compiler is known as reserved word
DeF#.02: The words that are used by a pogromming language for special purpose are called
reserved word.
Explanation: the reserved words are that words which are used in a computer programming
language for a specific purpose as these word are reserved for that special purpose. Each
keyword has fixed meaning and that cannot be change by the user. The reserved words
cannot be used as variable names in a program.
Example: int, If, Else, While, etc are reserved words in C,C++.
Question#.06 at exercise
Q: What is the basic structure of C Program? Explain different types of preprocessor
directives?
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.23
Ans. Basic structure of C program: A C language program consist of three basic main
parts which are given as below:
Preprocessor Directives
The main () function
Body of main{}
Preprocessor Directives: the instructions that are given to the compiler before the beginning
of the actual program are called preprocessor directives. These are also known as compiler
directives. The complier adds special code from these directives into the program at the time
of compilation. These preprocessor directives normally starts with the hash sign # and the
keyword like #include or # define . e.g #include<conio.h>
Main () function: when a c program is executed the control goes to Main() function as main
() function contain the statement of the c program which is known as the body of C program.
All the statements are written in the body of the C program are enclosed in braces {}. The
program cannot be compiled without main() function by the complier.
Syntax: Main()
{
Body of the program
}
Body of main: the statement of the program written inside the main () function in between
the braces {}. These statements are the body of the program. Each statements in C program
ends with semicolon;. C is case sensitive language. The statements are normally written I
lowercase letters.
Types of Preprocessor directives:
The commonly used preprocessor directives are given as below:
(i).<stdio.h>
Question#.07 at exercise
Q: What are the constant and variable, Differentiate them with examples.
Ans. Constant: constant is a quantity which cannot be changed during program execution.
Variable: A quantity which value change during program execution is known as variable.
Difference between Constant and variable
S/no Variable Constant
1 Variables are quantities with changing Constants are quantities with
magnitude, hence can assume different unchanging values, and used to
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.24
Int variable;
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.25
(III) character (char) type: a character variable can only store one character. An example of
declaration of character variable is
Char ch;
The C compiler sets aside one byte if memory for storing a character in a character variable.
Question #.10 at exercise
Q:What is type casting? Explain with examples.
Ans: Type Casting: Def: the process of converting one data type into another data type
during program execution is called type casting.
Types: type casting is performed in two ways,
(i)Implicit type casting
(II)Explicit type casting
(i)Implicit type casting: implicit type casting is performed automatically by the C compiler
itself.
Example: suppose x is an integer and y is a long variable and following expression is
evaluated
X+Y
In the above expression data type of x is lower than data type of Y, so the value of X will be
converted into long during the evaluation of expression.
(ii)Explicit type casting: Explicit type casting is performed by programmer himself, it cannot
be done by the complier automatically. The Cast operator is used to perform explicit type
casting, as this operator till the compiler to convert the data type of a given value.
Example: Suppose X and y are two float variables. X contain 10.3 and y contains 5.2 and the
following expression is evaluated.
X%Y
The above expression will generate an error because data type of x and y is float. The
modulus operator cannot work with float variable. It only works with integer, so the
expression can be written as below:
(int)X%(int)Y
The above statement converts the value of x and Y into integers and evaluate the expression.
The value of x will be converted to 10 and value of y will be [Link] the result of the
above expression will be 0.
What is oop: (Question#02(iv) at exercise )
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.26
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.27
Exercise
Question #.01 Select the most appropriate choice.
i. what is the set of instructions that till a computer what to do and how to do
called_____________.
a. compiler b. Program c. Comments d. Hardware
ii. Low level language are close to ________________.
a. Human language b. machine language c. computer hardware d. computer software
iii. which of the following modules is not part of c programming?
a. compiler b. Editor c. Linker d. Convertor
iv. The output of the compiler is called________________.
a. the program b. source code c. linked code d. object code
v. what is the type of language in which instructions are written in binary form
called_______________.
a. Machine language b .Assembly language c. High level language d. none
vi. Which of the following is not a high level language?
a. BASIC b. Assembly language c. Pascal d. FORTRAN
Vii. Which of the following is a compiler directive?
a. #include<stdio.h> b. Float x; [Link] main() d. All of these
viii. The process of converting source code into machine code is known as_____
a. Compilation b. Executing c. Linking d. Saving
Ix. A quantity whose value may change during execution of the program is called
a___________.
a. constant b. variable [Link] d. symbol
X. The execution of a C program starts from function.
a. Const() b. main() [Link] d. start
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.28
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.29
\t to print tab
Program 3.1:
Program to print a string of characters on a screen.
#include<stdio.h>
#include<conio.h>
Void main( )
{
clrscr( );
printf(“\n welcome to programming environment”);
printf(“\n this is my first program”);
getch();
}
OUTPUT
Welcome to programming environment
This is my first program
Program 3.2:
Program to print 1 2 3 4 using printf( ) function:
#include<stdio.h>
#include<conio.h>
Void main( )
{
clrscr( );
int a=1, b=2, c=3,d=4;
printf(“%d\t %d\t %d\t %d\t”,a,b,c,d);
getch( )
}
OUTPUT
1 2 3 4
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.30
Like a printf( ) function puts ( ) function can also use to display message on screen.
General format is:
Puts(string_variable-name);
Uses of puts( ) function:
It is use to printing message
Ask user to enter the data
It is also use to orienting result
Program 3.3:
Program to output a string on the screen by using puts( ) function.
#include<stdio.h>
#include<conio.h>
Void main( )
{
clrscr( );
puts(“my name is SAQIB QURESHI”);
getch( );
}
OUTPUT
My name is SAQIB QURESHI
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.31
#include<conio.h>
Void main( )
{
clrscr( );
int a=15, b=22, c, product;
cout<<”\n this program will add and multiply two numbers”<<endl;
c = a+b;
product= a*b;
cout<<”\n the sum of “<<a<<”and”<<b<<” is <<c<<endl;
cout<<”\n the product of “<<a<<”and”<<b<<” is <<a*b<<endl;
getche( );
}
OUTPUT
This program will add and multiply two numbers
The sum of 15 and 22 is 37
The product of 15 and 22 is 330
NOTE: endl is also work as a \n, and it is pronounced as End L.
INPUT FUNCTION:
Definition:
Input functions are those functions through which data is entered into the
computer. There are several ways to assign data to variable. The data can be assign to
the variable through input/assignment statement. The following input functions are:
a) scanf( ) function
b) getch( ) function
c) getche( ) function
d) getchar( ) function
e) gets( ) function
f) cin<< function
(a) scanf( ) function:
Definition:
Scanf( ) function is used to accept values for variables during the program
execution. The scanf( ) function inputs any type of data. The general syntax is:
scanf(“format specifier”, &var1,&variable2,…….);
where the var1 and var2 are the list of variables separated by commas, in which the
values are to be assign. The symbol & used to represent the address of memory
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.32
location. For example we input the values of two integers i-e a and b, so the scanf( )
function is written as:
scanf(“%d,%d”,&a,&b);
here %d is a format specifier for integer.
Program 3.5:
Program to input student’s roll number, total marks, obtained marks and
calculate percentage.
#include<stdio.h>
#include<conio.h>
Void main( )
{
Int rno, tm, obm
float percentage;
clrscr( );
printf(“\n Enter Roll number=:”);
scanf(“%d”,&rno);
printf(“\n Enter total marks=”);
scanf(“%d”,&tm);
printf(“\n Enter obtained marks=”);
scanf(“%d”,&obm);
percentage = obm*100/tm;
printf(“\n percentage=%d”, percentage);
getche( );
}
OUTPUT
Enter Roll number: 10
Enter total marks= 800
Enter obtained marks= 650
Percentage = 81.25
(b) getch( ) function:
Definition:
The getch( ) function is used to read a single character from the keyboard with
echoing it on the screen.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.33
The getch( ) is very important function that is used to display a single character
without showing the character on the screen.
General syntax is:
getch( );
program 3.6:
#include<stdio.h>
#include<conio.h>
Void main( )
{
clrscr( );
char ch;
printf(“enter character:”);
ch=getch( );
printf(“\n”);
putchar(ch);
getch( );
}
OUTPUT
Enter character:
S
Definition:
This function is used to read and display a single character from the keyboard
with echo on the screen.
The getche( ) function is display a single character from the keyboard but its shows
the character on the screen.
getche( );
program 3.7:
#include<stdio.h>
#include<conio.h>
Void main( )
{
clrscr( );
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.34
char ch;
printf(“enter character:”);
ch=getche( );
printf(“\n”);
putchar(ch);
getche( );
OUTPUT
Enter character: S
Definition:
The getchar( ) function is used to read a character from the keyboard with echo on the
screen by pressing the ENTER key.
Explanation:
The getchar( ) function work as a getche( ) function but it requires the pressing
ENTER key after entering the character.
Syntax is:
getchar( );
Program 3.8:
#include<stdio.h>
#include<conio.h>
Void main( )
{
clrscr( );
char ch;
printf(“enter character:”);
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.35
ch=getchar( );
printf(“\n”);
putchar(ch);
OUTPUT
Definition:
The gets( ) function reads in an entire string until user presses ENTER key.
Explanation:
Whenever gets( ) statement encounters then characters entered by user will be copied
into the variable. If user starts accepting characters and if new line character appears
then the newline character will not be copied into the string variable i-e name.
Program 3.9:
#include<stdio.h>
#include<conio.h>
Void main( )
{
char name[20];
clrscr( );
gets(name );
puts(name);
getch( );
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.36
OUTPUT
Abbottabad
Program 3.10:
Write a program to get name and age (in years) of a person. Calculate the age in
month and print the name of the person and its age in month.
#include<stdio.h>
#include<conio.h>
Void main( )
{
int age, age_month;
char name[40];
clrscr( );
gets(name );
scanf(“%d”, &age);
age_month=age*12;
getche( );
OUTPUT
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.37
cin>> variable1>>variable2>>……>>variable n;
like a scanf( ) function cin does not require the & operator at the start of the
variable name.
Program 3.11:
clrscr( );
cin>>ftemp;
getche( );
OUTPUT
Statement terminator:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.38
Definition:
The statement terminator is used at the end of the statement. Its indicates the
compiler that the statement ends here. If the statement terminator is not used at the
end of the statement, then the C compiler give an error message during the
compilation of the program.
For example, in the clear screen statement we write this statement as:
clrscr( );
Format specifier:
Definition:
Format Purpose
specifiers
% ld Long integer
%c Signed character
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.39
%s String
The %d format specifier is used to Read and print an integer and %id is used for long
integer.
Program 3.12:
#include<stdio.h>
#include<conio.h>
Void main( )
Clrscr( );
Printf(“a= %d \n”,a);
Printf(“b= %d \n”,b);
getche( );
The %f , %e and %g is used to Read and print floating point format specifier.
%f format specifier is used for floating point (decimal notation) , %g for exponential
notation and %e for exponential notation.
Program 3.13:
#include<stdio.h>
#include<conio.h>
Void main( )
Int a;
float b;
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.40
clrscr( );
a = 15;
b = a/2;
Printf(“a= %d \n”,a);
Printf(“b= %f \n”,b);
Printf(“b= %e \n”,b);
getche ( );
OUTPUT
a = 15
b = 7.5
b = 7.5 e+0
Program 3.14:
#include<stdio.h>
#include<conio.h>
Void main( )
Char ch=’A’;
Clrscr( );
Printf(“character = %c\n”,ch);
getche( );
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.41
OUTPUT
Character = A
Escape sequence:
\a Bell (alert) cause a beep sound Printf(“Hello \a how r Beep after hello and
in the computer internal you”); before how are you.
speaker.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.42
Operators:
Definition:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.43
Assignment operator:
Definition:
The assignment operator is used to assign the values to the variables. It is denoted by
“=”.
Explanation:
The assignment operator is used to assign numeric or character values. However,
string data cannot be assigned to a variable with this operator. General format for the
assignment operator is:
Variable = character_constant /variable/expression;
Few examples of assignment operators are as follows:
Rno =103;
Percentage = 76.5;
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.44
Gender = ‘M’
Area = (3.14)*r*r;
It is important to note that a value assigned to a variable must be of the same type as
that of the variable type.
Compound operators:
Definition:
Compound operator is used to perform mathematical operations more easily.
The compound assignment operator combines the simple assignment operator with
other binary operator.
Explanation:
Compound assignment operators provide a shorter syntax for assigning the result of
an arithmetic operator. They perform the operation on the two operands before
assigning the result to the first operand. There are eleven compound assignment
operators.
The general syntax is:
Variable op= expression;
Or a+= 10; it is equivalent to a = a + 10;
a - = 10; it is equivalent to a = a - 10;
a*= 10; it is equivalent to a = a * 10;
program 3.15: Write a program that perform all compound assignment
operations on an integer.
#include<stdio.h>
#include<conio.h>
Void main( )
{
Int a;
a = 10;
printf(“\n the value of a = %d”,a);
a += 5;
printf(\n“value of a after a += 5 = %d”,a);
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.45
a - = 5;
printf(\n“value of a after a - = 5 = %d”,a);
a * = 2;
printf(\n“value of a after a *= 2 = %d”,a);
a / = 2;
printf(\n“value of a after a /= 2 = %d”,a);
a % = 2;
printf(\n“value of a after a %= 2 = %d”,a);
getche( );
}
OUTPUT
Value of a = 10
value of a after a += 5 = 15
value of a after a - = 5 = 10
value of a after a *= 2 = 20
value of a after a /= 2 = 10
value of a after a %= 2 = 0
Increment operator:
Definition:
Increment operators are used to increase the value or add the value to the
variable by 1. The increment operator is represented by ++. It is also a unary operator.
Explanation:
The increment operators are used either of after variable. If the increment
operator written before the variable, it is called prefix increment operator. And if the
increment operator written before the variable, it is called postfix increment operator.
For example, if we add 1 to the value of variable x, then it will be written as”
X = X +1; Or
X ++; Or ++X;
Prefix increment operator:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.46
Definition:
When an increment operator is used in prefix (before the variable) in an expression. It
adds 1 to the value of the variable before the value of the variable is used in
expression.
For example:
Sum = a + (++b);
Program 3.16:
#include<stdio.h>
#include<conio.h>
Void main( )
{
Clrscr( )
Int a, b, c, sum;
a= 1;
b=1;
c=3;
sum = a + b +(++c);
printf(“\n sum = %d”,sum);
printf(“\n c= %d”,c);
getche( );
}
OUTPUT
Sum = 6
C=4
In this program 1 value will be added to c before it is used in the expression. So the
sum is 6 and the value of c is 4.
Postfix increment operator:
Definition:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.47
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.48
a= 1;
b=1;
c=3;
sum = a + b +(--c);
printf(“\n sum = %d”,sum);
printf(“\n c= %d”,c);
getche( );
}
OUTPUT
Sum = 4
C=2
In this program 1 value will be subtracted from c before it is used in the expression.
So the sum is 4 and the value of c is 2.
Postfix decrement operator:
Definition:
When a decrement operator is used in prefix mode (after the variable) in an
expression, it subtracts 1 to the values in the variable after the value of the variable is
used in expression.
For example:
Sum = a + b + (c--);
Relational operators:
Definition:
The operators which compare two operands (Values) and give result as
true/false or equal, less/greater etc… is called relational operators.
Explanation:
A relational operator compares two values. It produced true or false. The relational
operator allow us to compare two values to check whether they are equal to each
other, unequal or whether one is greater than other.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.49
> is greater than the value on the right side, otherwise returns
false.
less than operator returns true if the value on the left side of <
< is less than the value on the right side, otherwise returns false.
== Equal to operator returns true if the values on both side of ==
are equal otherwise, returns false.
>= Greater than or equal to operator returns true if the value on the
left side of > = is greater than or equal to the value on the right
side, otherwise returns false
Less than or equal to operator returns true if the value on the
<= left side of < = is less than or equal to the value on the right
side, otherwise returns false
!= Not equal to operator returns true if the value on the left side of
! = is not equal to the value on the right side, otherwise returns
false
Logical operators:
Definition:
Logical operators are used to evaluate compound condition. The logical
operator is used to perform logical operations.
Explanation:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.50
In C language there are three logical operator AND , OR,NOT are used to form
compound conditional expressions in which more than one logical condition are
tested in the same statement. A condition in C evaluate to either true or false which
instructs the computer to do a specific task. These operators are as below:
i. And operator
ii. OR operator
iii. Not operator
i) AND operator: it is used for logical multiplication. It is used to combine two or more
expressions (conditions) and thus form a compound expression. if all conditions
are true then the result will be true otherwise the result will be false.
Condition 1 Operator Condition 2 Result
False && False False
False && True False
True && False False
True && True True
For example, we have two variables i-e a= 100 and b= 50 then the compound
condition:
(a=10) && (b=10) is true because both conditions are true.
And if (a>50) && (b=>50) is false because second condition is false.
ii) OR operator: It is used for logical addition. In this operator if one of the given
condition is true then the result will be true. If both the condition is false, then the
result will be false.
Condition 1 Operator Condition 2 Result
False || False False
False || True True
True || False True
True || True True
For example, we have two variables a= 100 and b = 50,
The condition (a>50) || (b = >50) is true, as one condition is true.
iii) NOT operator: it reverts the condition from true to false and from false to true.
Operator Condition Result
! True False
! False True
For example, we have two variables a= 100 and b = 50,
The condition !(a == b) is true, as a is not equal to b.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.51
Difference between the assignment operator (=) and equal to operator (==):
The assignment operator “=” is used to assign a value to a variable whereas the
equal to operator “==” is used to compare two value of the same data type.
For example:
X = 15;
Y = a;
Z = ‘s’;
In the above statements, variable X is assign the value 15, y is assign the value stored
in a variable “a” and c is assign the character ‘s’.
The relational operator == is used to build a condition based on which computer takes
some action.
For example:
b == 10;
x == y;
in this case the value of b is equal to 10 is true otherwise false. And in second
condition the equal to operator checks whether the value of x is equal to the value of
y. if the value of x is equal to the value of y than it will be true otherwise false.
Difference between Unary and Binary operators:
Unary operator:
Definition:
An operator that works with single operands is called Unary operator.
OR a unary operator operates with only one operands i-e an operation with a single
input, or in other words, a function of a one variable.
For example:
-, ++ , --
As these operators are used with operands i-e
-a
n++
--x
Binary operator:
Definition:
An operator that works with two operands is called Binary operator.
OR A binary operator operates with two operands i-e an operation with two inputs,
or the function perform by two variables.
The binary operators are:
+, -, *, /, %
For example:
X+y
X–y
y/z
x*y
x%y
Conditional operator (?):
Definition:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.52
The conditional operator is also called ternary operator. It works with three
operands/variables. The conditional operator is also called decision making structure.
Syntax:
(condition) ? true-case statement: false-case statement;
Condition: the condition is specified as relational or logical expression.
True-case: It is executed if expression evaluates to true.
False-case: It is executed if expression evaluates to false.
For example:
Suppose we have a variable a=5 then the following statement is:
X=(a > 50) ? 1 : 0;
This will assign 1 to x if the condition a>50 is true otherwise it will assign 0 to
x if the condition is false. As a > 50 is false so x=0.
Operator and their precedence:
Operator precedence determines the group of terms in an expression and decides how
an expression is evaluated. Some operators have higher precedence than other and
some lowest precedence.
The following table show the operator and their precedence:
Precedence Operators Type
1 ! Logical NOT
2 *, /, % Multiplication, Division &
Modulus
3 +, - Addition & Subtraction
4. <, >, <=, >= Relational operators
5 ==, != Relational operator
6 && Logical AND
7 || Logical OR
8 = Assignment operator
Exercise
Q.1: Select the most appropriate choice.
1. The function that are used to get data and then to assign it to variables are known
as______________.
a. Output functions b. String functions c. Numeric functions d. Input
functions
2. which of the following functions is used to display output on the screen_____________.
a. Printf( ) b. scanf( ) c. gets( ) d. getchar( )
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.53
a. + b. = c. -- d. ++
9. Which operator is used to add 1 to the value of the variable after the value of the variable
has been used in the expression?
a. post fix increment b. prefix increment c. Modulus operator d. Binary operator
10. which operator is used to produce true result if both conditions are true.
a. AND b. OR c. NOT d. All of the above
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.54
Ans (c):
100 % 50 – 100 % 3
0 - 1
-1
Vi Differentiate between simple and compound assignment operators.
(see answer already explained )
Q.3: What are operators? Explain different types of operators with examples.
(see answer )
Q.4: What is output functions? Describe in detail different types of output functions.
(see answer )
Q.5: Discuss different format specifiers and escape sequences used in printf( ) functions.
(see answer )
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.55
#include<conio.h>
Void main( )
{
float b, h, area;
printf(“\n enter the value for base and height”);
scanf(“%f %f”,&b, &h);
area = (b*h) / 2;
printf(“area is =%f\n”,area);
getche( );
}
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.56
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.57
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.58
Statement;
The above syntax is used for single statement. If we have multiple statements then it
will written between two braces { }. It is also called block of statements.
The syntax of if statement when statements in a bock, written as:
If(condition)
{
Statement 1;
Statement 2;
.
.
.
Statement n;
}
Structure of if statement:
If True
statement
Statements (s)
False
Program 4.1: A program that prints the entered number when it is greater than
50.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int x ;
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.59
Clrscr();
Printf(“\n enter an integer =”);
Scanf(“%d”, &x);
If(x>50)
Printf(“%d”, x);
getch();
}
OUTPUT
Enter an integer = 75
75
Program 4.2:
The following program executes a single statement if the given condition is
true.
#include<stdio.h>
#include<conio.h>
Void main( )
{
Int a, b;
Clrscr( );
a = 1000;
b = 500;
if (a>b)
printf(“\n value of a is greater than b”);
getche( );
}
OUTPUT
Value of a is greater than b
Program 4.3:
Program that inputs two numbers and print the message if both are equal.
#include<stdio.h>
#include<conio.h>
Void main( )
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.60
{
Clrscr( );
Int a, b;
Printf(“\n enter first number:”);
Scanf(“%d”, & a);
Printf(“\n enter second number:”);
Scanf(“%d”, & b);
If (a==b)
Printf(“both numbers are equal”);
getche( );
}
OUTPUT
Enter first number: 50
Enter second number: 50
Both numbers are equal
If –else statement:
Definition:
If-else statement is used to executes on statement or a group of statement until
the condition is true or false.
In the if-else statement if one condition is executed and become true than other
statement will be false.
Syntax of if-else statement:
If(condition)
Statement;
else
statement;
when two or more statements are used than it can be written in braces { }.
The syntax of compound if-else statement is written as:
If(condition)
{
Statement1;
Statement 2;
.
.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.61
Statement n;
}
else
{
Statement1;
Statement 2;
.
.
Statement n;
}
False True
If
condition
Program 4.4: A program that inputs a number and find whether it is even
or odd using if-else structure.
#include<stdio.h>
#include<conio.h>
Void main( )
{
Int n;
Clrscr( )
Printf(“Enter a number:”);
Scanf(“%d”, &n);
If(n%2 ==0)
Printf(“%d is even”, n);
else
printf(“%d is odd”,n);
getch( );
}
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.62
OUTPUT 1
Enter a number: 10
10 is even
OUTPUT 2
Enter a number: 17
17 is odd
OUTPUT
Enter two numbers: 15 20
20 is larger than 15
Program 4.6: A program that finds the smallest number of three numbers:
#include<stdio.h>
#include<conio.h>
Void main( )
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.63
{
Int x, y, z, s;
Clrscr( )
Printf(“Enter Three numbers:”);
Scanf(“%d %d %d”, &x, &y, &z);
If(x < y)
S = x;
Printf(“the smaller number is: %d”, s);
else
s=y
if(s <z)
printf(“the smaller number id %d:”, s);
else
printf(“the smaller number is %d:”, z);
getch( );
}
OUTPUT
Enter three numbers: 10 6 12
The smaller number is: 6
Switch statement:
Definition:
This statement is used for multiple choice or selection. It is a substitute of if-
else statement. It is also used when multiple choices are given and one is to be
selected.
Explanation:
A switch statement allows a variable to be tested for equality against a list of
values. Each value being switched on is checked for each switch case. The switch
statement evaluates an expression and return a value. Only one of the case in the
switch statement is executed depending upon the value returned by the expression.
The syntax is used for switch statement as:
Switch (expression)
{
Case 1:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.64
Statements;
break;
case 2:
statements;
break;
.
.
.
Case n:
Statements
break;
default:
statements;
}
In the syntax, expression appears within the switch statement. First the expression is
evaluated and then the value returned by the expression is compared with each case
number. If it matches with case number than the statement of those case will be
executed.
The break is used to exit from switch body. It is used at the end of each case.
The keyword default used in the body of the switch statement. The default used when
no match in each case, under this condition the default is executed.
False
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.65
False
Default
statements
No match
Next statement/s
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.66
break;
Case ‘u’:
Printf(“Vowel u”);
break;
default:
Printf(“Not aVowel”);
}
getche( );
}
OUTPUT
Enter a character: a
Vowel a
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.67
Program 4.11: A program to find that a number that is input from the keyboard is
Positive, negative, even, or odd?
#include<stdio.h>
#include<conio.h>
Void main ( )
{
Int num;
Printf(“Enter a number:”);
Scanf(“%d”, & num);
If(num>0)
{
Printf(“\n you enter positive number”);
If(num%2==0)
Printf(“\n You enter even number”);
else
printf(“\n you enter odd number”);
}
Else
Printf(“you enter negative number”);
getche( );
}
OUTPUT
Enter a number: 7
You enter positive number
You enter odd number
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.68
Exercise
Q#1: Select the most appropriate choice.
1. Which programming statement makes a decision?
a. { } b. ( ) c. [ ] d. < >
Q#2: Write a program in C to input three integers. Find out the largest among
these integers using if-else structure and print it on the screen.
Ans: #include<stdio.h>
#include<conio.h>
Void main( )
{
Int x, y, z, largest;
Clrscr( )
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.69
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.70
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.71
getche( );
}
OUTPUT
Enter a character: a
Vowel a
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.72
if (a>10)
printf(“value is greater than 10”);
else if
printf(“value is less than 10”)
}
Ans:
In line number 5 the output statement not defined. It’s a syntax error. Print to
printf ().
In line number 6 the format specifier %f is not required because we declare
integer format specifier. So %d is correct format specifier to use in scanf( )
function.
In line number 10, the statement terminator is missing. (;)
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.73
Loop structure:
Loops cause program to execute the certain block of code repeatedly until test
condition is false. Loops are used in performing repetitive task in programming.
Consider these scenarios:
You want to execute some code/s 100 times.
You want to execute some code/s certain number of times depending upon input from
user.
These types of task can be solved in programming using loops.
There are 3 types of loops in C programming:
for loop
while loop
do...while loop
1. The for loop:
Definition:
The for loop executes one or more statement for a specified number of times. It is a
more frequent loop used by the programmers.
Explanation:
Syntax:
for(initialization; condition; increment/decrement)
{
Body of the loop;
}
Initialization:
It is an expression which specifies the starting value of the counter variable.
Condition:
It is a relational expression. The statement within a loop is executed only if the given
condition is true. If condition is false, the statement will not be executed.
Increment/Decrement:
This part of a loop specifies the change in counter variable after each execution of the
loop.
Body of the loop:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.74
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.75
Program 5.3:
A program that inputs a number and displays the table of the number up
to 10 using for loop.
#include<stdio.h> OUTPUT
#include<conio.h> 5x1= 5
void main( ) 5 x 2 = 10
{
5 x 3 = 15
Int tab,n,x;
5 x 4 = 20
clrscr( );
5 x 5 = 25
printf(“\n enter number for the table”);
5 x 6 = 30
scanf(“%d”,&n);
for(x=1;x<=10;x++) 5 x 7 = 35
printf(“\n%d x %d=%d”,n,x,n*x); 5 x 8 = 40
getche( ); 5 x 9 = 45
} 5 x 10 = 50
Program 5.4: A program to calculate the sum of odd numbers from 1 to 20 and
thn print the sum on the screen.
#include<stdio.h>
#include<conio.h>
Void main( )
{
Int n, sum;
Clrscr( );
Sum = 0;
For(n=1; n<=20; n+=2)
{
Sum = sum + n;
}
Printf(“\n sum =%d”, sum);
getche( );
}
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.76
OUTPUT
Sum = 10
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.77
clrscr ( );
n = 1;
while (n<=5)
{
Printf(“\n Pakistan”);
n++;
}
getche ( );
}
OUTPUT
Pakistan
Pakistan
Pakistan
Pakistan
Pakistan
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.78
}
getche ( );
}
OUTPUT
10 9 8 7 6 5 4 3 2 1
Program 5.7: A program that resolve the following series and displays the
result using while loop.
2+4+6+………….+20
#include<stdio.h>
#include<conio>
void main ( )
{
Int c, sum=0;
clrscr ( );
c = 2;
while(c<=20)
{
Sum = sum+c;
C = c+2;
}
Printf(“sum = %d”, sum);
getch ( );
}
OUTPUT
Sum = 110
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.79
Explanation:
The do-while loop is similar to the while loop, except that the test condition occurs at
the end of the loop. Having the test condition at the end, guarantees that the body of
the loop always executes at least one time.
The do-while loop is an exit-condition loop. This means that the body of the loop is
always executed first. Then, the test condition is evaluated. If the test condition
is TRUE, the program executes the body of the loop again. If the test condition
is FALSE, the loop terminates and program execution continues with the statement
following the while.
Syntax of do-while loop is:
do
{
body f the loop;
}
while(condition);
where
do: it is the keyword that indicates the beginning of the loop.
Condition: The condition is given as a relational expression. The statement of the
body is executed if the given condition is true. If the condition is false, the statement
of the body are executed only once because the condition comes at the end.
Body of the loop: It is a statement or group of the statements in braces{ } to be
repeated.
Program 5.9: A program to print first ten integers using do-while loop.
#include<stdio.h>
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.80
#include<conio.h>
Void main ( )
{
Int n;
clrscr( );
n =1;
do
{
Printf(“\t %d”, n);
n ++;
}
While( n<=10);
getche( );
}
OUTPUT
1 2 3 4 5 6 7 8 9 10
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.81
Program 5.10:A program that inputs a number from the user using for loop. If
the number is greater than 0, it is displayed and the next number is input. The
program exist the loop if the number is 0 or negative using break statement.
#include<stdio.h>
#include<conio.h>
void main ( )
{
Int x, num;
clrscr( );
for(x =1; x<=5; x ++)
{
Printf(“\n Enter a number”);
Scanf(“%d”, &num);
If(num<=0)
break;
printf(“\n you entered %d\n”,num);
}
Printf(“\n OK”);
getche( );
}
The above program uses break statement in for loop. The counter variable x indicates
that the loop should execute for five times. But It is executed only twice.
Explanation:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.82
Program 5.11: write a program and use the concept of continue statement
using while loop.
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int c;
c =1;
while(c<=5)
{
Printf(‘\n Pakistan”);
c ++;
continue;
printf(“Islamabad”);
printf(“Peshawar”);
}
getche( );
}
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.83
OUTPUT
Pakistan
Pakistan
Pakistan
Pakistan
Pakistan
Nested Loop:
Definition:
A loop within another loop is called nested loop. In nested loop an inner loop is
completely enclosed inside an outer loop.
Explanation:
The depth of nested loop depends on the complexity of a problem. We can have any
number of nested loops as required. Consider a nested loop where the outer
loop runs n times and consists of another loop inside it. The inner loop runs m times.
Then, the total number of times the inner loop runs during the program execution is
n*m.
Program 5.12: A program to print the output as shown below. Using nested
for loops.
#include<stdio.h>
Include<conio.h>
void main ( )
{
Int i , j;
clrscr ( );
for(i=1; i<=5; i ++)
{
for(j=1; j<=I; j++)
printf(“\t %d”, j);
}
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.84
printf(“\n”);
}
getche( );
}
OUTPUT
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
EXERCISE
Q.1: Select the most appropriate choice.
i. How many types of loop structures are available in C?
a. 4 b. 3 c. 2 d. 6
ii. Which loop structure always executes at least once
a. do-while b. for c. while d. nested
iii. in which loop the condition comes before the body of the loop?
a. While loop b. do-while loop c. for loop d. a and c both
iv. In which loop the condition comes after the body of the loop?
a. for loop b. While loop c. do-while loop d. a and c both
v. Which of the following statement is used in a body of the loop to exit from the loop?
a. break b. terminate c. exit d. both a and b
vi. a loop within another loop is called___________.
a. Counter loop b. complex loop c. outer loop d. nested loop
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.85
vii. Which statement is used o move the control to the start of loop body?
a. Continue b. break c. while d. for
viii. Which of the following loop is a good choice when the number of iterations are
known in advance.
a. Do-while b. for c. while d. nested
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.86
2 4 6 8 10 12 14 16 18 20 22 24 26
28 30 32 34 36 38 40 42 44 46 48 50 52
54 56 58 60 62 64 68 70 72 74 76 78 80
82 84 86 88 90 92 94 96 98 100
Prod= 6.84 X 1079
OUTPUT
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Q.6: Write a program to display the following output using nested for loop.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.87
a
a a
a a a
a a a a
a a a a a
Ans: #include <stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
getch( );
}
Q.7: Write a program in C to print the sum of odd numbers from 1 to 50 using for
loop.
Ans:
#include<stdio.h>
#include <conio.h>
void main( )
{
int i, n, sum=0;
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.88
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.89
CHAPTER # 06.
Data:
Raw facts and figures which are processed by digital computer is called data.
OR Data is a collection of facts, such as numbers, words, measurements, observations or
even just descriptions of things.
OR the quantities, characters, or symbols on which operations are performed by a
computer, which may be stored and transmitted in the form of electrical signals and
recorded on magnetic, optical, or mechanical recording media.
Data representation:
Data is represented in a computer by means of simple on/off switches, and
digitally these become 1 and 0. Millions of switches in combination create all the
data in a computer system.
All the data entered into computer is first converted into binary number system i-e 0
and 1. One digit in binary number is called a Bit and the combination of eight bit is
called Byte.
A byte is a basic unit of computer memory that is used to represent data.
For example, when a letter A is input to the computer it is first converted to its binary
code i-e 01000001.
Logic gates:
Logic gates are the basic building blocks of digital computer. Logic gates operates on
two voltage levels and process digital signals which represent binary digit 0 and 1.
Digital Logic and Logic gates:
Digital logic is used to create a digital circuit which consist of large number of logic
gates:
A logic gate is an elementary building block of a digital circuit. Most logic gates have
two inputs and one output. At any given moment, every terminal is in one of the two
binary conditions low (0) or high (1), represented by different voltage levels.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.90
Definition:
AND gate perform the logical multiplication, commonly known as AND function.
Explanation:
The AND gate has two or more inputs and a single output. The output of AND gate is
high (1) when all its inputs are high, and if any one input is low (0) then output will be
low.
Here we have two inputs A and B, then output F can be represented mathematically
as:
F= A . B
Here (.) dot is denoted the AND operation.
Symbolically it is represented as:
A
F=A.B
B
OR Gate:
Definition:
The or gate performs logical addition, commonly known as OR function.
Explanation:
The OR gate has two or more inputs and single output. The output of OR gate is high
when any one of its input is high.
Here we have two inputs A and B, then output F can be represented mathematically
as:
F=A+B
Here (+) sign denotes OR operation.
Symbolically it is represented as:
A
F=A+B
B
OR Gate:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.91
Definition:
NOT Gate is used to reverse the input value. It is a gate whose input is the inverse or
compliment of the input.
Explanation:
NOT gate takes single input and produces a single output. If the input to NOT gate is
“0” then the output will be “1”. If the input is “1” then the output will be “0”. It is also
performing the logical negation. NOT gate inverts the inputs to produce the output
therefore, it is also called inverter.
Symbolically is represented as:
X F=X
Truth table:
Definition:
It is a table which contains all the possible combinations of input values and their
corresponding output values.
Explanation:
In a truth table, there is “n” input values then there will be 2n possible combinations. It
is also used to represent all possible combinations of a Boolean function or expression
or logic gate.
Boolean function/expression is the combination of Boolean variables and/or
constants linked by Boolean operators.
Properties of truth table:
Truth table consists of rows and columns.
It shows relationship between input to output from digital logic circuit.
It shows output for all possible combinations of input using 0 for low and 1 for
high.
All the combinations of inputs are listed in columns on the left and outputs is
shown in the rightmost column.
The input columns are constructed in the order of binary counting with a
number of bits equal to the number of inputs.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.92
X Y F=X.Y
0 0 0
0 1 0
1 0 0
1 1 1
X Y Z F x.y.z
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1
X Y F=X+Y
0 0 0
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.93
0 1 1
1 0 1
1 1 1
X Y Z F x.y.z
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
X F = X/
0 1
1 0
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.94
NOR gate is actually the compliment or inverse of the OR gate. Therefore, it is also
called NOT(OR) gate.
If X and Y are two inputs, then F can be represented mathematically as:
Y
Truth table for NOR gate:
X Y
0 0 1
0 1 0
1 0 0
1 1 0
NAND Gate:
Definition:
The gate whose output is “0” when all the inputs are “1” is called NAND gate.
Explanation:
NAND gate is actually the compliment of the AND gate. Therefore, it is also called
NOT(AND) gate.
If X and Y are the two inputs to a NAND gate and F is its output, then symbolically it
represented as:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.95
X Y
0 0 1
0 1 1
1 0 1
1 1 0
INPUTS OUTPUT
X Y F = (X Y) = X/Y + XY/
0 0 0
0 1 1
1 0 1
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.96
1 1 0
X F = (X Y) = X/Y + XY/
Y
INPUTS OUTPUT
X Y F = (X Y) = XY + X/ Y/
0 0 1
0 1 0
1 0 0
1 1 1
Creating NAND, NOR, XOR and XNOR gates using basic gates:
The NAND, NOR, XOR and XNOR gates can be easily created using AND, OR and
NOT gates.
Creating NAND and NOR gates using basic gates:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.97
The NAND gates can be easily created using an AND gate and a NOT gate as shown
below.
The NOR gate can also be created in a similar way by using an OR gate and a NOT
gate as shown below.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.98
Example 2:
Conversion of Boolean function F2 = X + Y/ Z to logic circuit.
To creat a logic circuit of this function, one AND gate is required for the term Y/ Z,
one Not gate to covert Y to Y/ and an OR gate to perform OR operation on the term X
and Y/ Z.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.99
Example 3:
Conversion of Boolean function F3 = XY/ + X/Z to logic circuit.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.100
Now implement these values of the truth table in the following K-Map.
A A/ A
B
0 1
B
B 0 1
A
Z = f(A,B) = A
Example 2:
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.101
Now implement these values of the truth table in the following K-Map.
A A/ A
B
1 1
B
B 1 0
A/ B/
Z = f(A,B) = A/ + B/
Simplification of three variables Boolean function by K-Map:
Example 1:
Z = f(A,B,C) = A.B.C + A/. B.C + A.B.C/ + A/.B.C/
Now drawing K-Map for the expression.
A A/B/ A/ B AB AB/
0 1 1 0
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.102
B
0 1 1 0
C
C/
Z = f(A,B,C) = B
Example 2:
SEE FROM BOOK
EXERCISE
Q.1: Select the most appropriate choice.
1. when the data enters into the computer, it is converted into _______?
a. binary number system b. Decimal number system
c. Hexadecimal number system d. Octal number system
2. An electronic circuit that takes one or more input signals and produces a single
output is called______________.
a. Diagram b. Table c. Digital logic circuit d. none of these
3. the output of which gate is 1 only when all its inputs are 1?
a. AND gate b. OR gate c. NOT gate d. NAND gate
4. which gate perform the logic negation of complementation?
a. AND gate b. OR gate c. NOT gate d. XOR gate
5. the gate whose output is “1” when all the inputs are 0’s is called____?
a. NAND gate b. NOR gate c. NOT gate d. XOR gate
6. K-Map method is not used for_______________________?
a. minimizing the Boolean expression
b. representing Boolean expression diagrammatically
c. making the comparison between two variables
d. designing the digital circuit
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.103
A/ A/ B /
B/
C/ + A/B/
C/
Q.5: simplifying the following Boolean function using K-map method. Also draw the logic
diagram of the simplified function.
Ans: (i) F = X/Z + X/Z/
Now implement these values of the truth table in the following K-Map.
X X/ X
Z
1 0
Z/
Z 1 0
X/
Z = f(X,Z) = X/
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.104
X X/ X/ Z
Z
X/ Z + X / Z
X/ X / Z/
(ii) F = A/ B + B C/ + B/ C/
F = A/ B (C + C/) + BC/ (A + A/) + B/C/(A + A/)
F = A/ BC + A/BC/ + ABC/ + A/BC/ + A B/C/ + A/ B/C/
Now drawing K-Map for the expression.
A AB AB/ A/ B A/B/
B
0 1
C
C/ 1 1 1 1
=A/B+C/
A
B
C
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.105
XY X/ Y XY/ X / Y/
1 0 0 O
Z
Z/
1 1 1 1
=XY+Z/
Logic Diagram :
X XY
Y
Z
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.106
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.107
All the web browser have graphical user interface which enable user to open multiple
websites at the same [Link] that web browser is very easy to learn and use.
Example:
Internet Explorer
Google Chrome
Opera mini
UC browser
Question : Define web server ?
Ans :Web Server :
Definition :“ A web server are computer that facilitate and deliver web pages from the
various websites that have ”
OR
“ Web server is computer that runs websites ”
Explanation :A web server is any internet server that respond to any http request and deliver
web page files etc. The web server includes hardware , software, protocol and contents.
Every web server has an IP address.
Question : Define URL ?
Ans :Uniform Resource Locator (URL) :URL is also called web address. It is an address of
a web page on the web/ internet. As each webpage has a unique address called web address
or URL. when want to create display/load a website. We must write the URL of that website
in the address bar of web browser.
Format :
URL have the following format
Protocol : / / Domain name / other information
Example :The URL of Mardan education board is http : / / [Link] / information
Where http (hyper text transfer protocol) is the protocol which specify how communication
will take place bisem is the domain name further web page is specified after domain name
As http : // [Link] /main / result will display the result page.
Question : Define and explain search engine ?
Ans :Search Engine :
Definition :“A search engine is a web site that allow user to find information on the World
Wide Web
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.108
Explanation : Search engine have been designed to help users in finding information on the
web. Users simply write a text word or sentence in a search engine then the search engine
looks for matching websites for all over the world on the web. The users open a website
according to his/her well or need by just clicking on displayed website. Finding or getting a
specific kind of information within millions of web pages on the web is difficult or
impossible task. But search engine have been solved this problem.
Example : Google is a best example of search engine which is commonly in use today.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.109
Explanation:These web sites publish new stories and let their visitors voice be heard. these
web sites provide their visitors a way to get their thoughts and views publish. very often at
the end of news stories, visitors are asked to share their experience or knowledge about the
topic.
Example:News websites like [Link]/ ,[Link]
,[Link] etc. .
Information websites: The web sites which provide information on any topic
In the form of text, videos and graphics etc.
Example: [Link] informational websites gives information on large
scale which include animals ,business, health ,education etc. .
Educational websites: those websites which are design for educational purposes
Explanation:These sites contain animation, slide presentation and tutorials to educate
people on various topic. information is presented in a well-organized way. its purpose is to
impart knowledge to people who r looking for better education.
Example: [Link] ,[Link] etc…
Personal websites:Those websites created for personal use which contain. There
information, hobbies, friends etc. Personal websites contain the information about that
person. its purpose is to share the personal information with family members and friends.
personal websites are also open for public and anybody can visit and leave comments.
Business Websites:These sites provide facilities to maintain business relationship and
selling information, services and commodities via internet .it give facilities to exchange the
business documents, such an orders or invoices b/w suppliers and customers. services
provide by business sites include banking, ticket, stock market transactions and even remote
education.
Entertainment Websites: It provides different types of entertainments to computer [Link]
include sport, radio, computer, games, fashion etc.
Write note on HTML?
INTRODUCTION TO HTML: HTML stand for hypertext markup language. it is a kind
of language which contain a series of special codes. they define the style that should be used,
when displaying the text and also allow the links to other documents .it is use to create
webpages. It is the language of WWW. Its codes are written using HTML tags. The tags are
special control character which are enclosed with in angle brackets (“< >”). Tags are
instruction tells your browser what to show on the web page .HTML files are plain text files
so they can be created by using single editor i – e: notepad, world pad etc.
Q:How an HTML document can be created and displayed
CREATING AND DISPLING HTML DOCUMENT; Note pad ++ is used for creating
HTML documents. it can be download from internet.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.110
Creating the first HTML document: Following are some steps to create a document in
HTML 1) Open the NOTEPAD ++ program and type the text.
2)now save your HTML document, open file menu and select save As …… and give file
name with the extension of .html.
Displaying the HTML document (web page):To open your document in browser window,
go to location where u save it. double click to open it. the browser window will be displayed.
Q:Describe basic tags used in HTML code.
Tags used to MARK – UP HTML, Element:
HTML Head and Body Tags: Following are the tags used for mark-up HTML element in
the document.
<html></html> Tags: The first one <html> use for the start of HTML document. and the
</html> indicates the end of document.
<head>, </head>Tags: It is use to give title to the webpage.
<body></body> tags: Any type of data inside the body will be display on browser
If any change is required, it can change easily by opening it.
Q:How text can be formatted while displaying on web using html?
TEXT FORMATTING: It is related to the appearance of the text in a web i-e size, color,
style
Basic of Text Formatting: Following are the basics of text formatting
Specifying a Page Title: The “<title></title>” tags are used to specify page title. The text
b/w them is use to set the title to the page which is displayed on the title bar on the web
browser.
Example:<title> THE SCHOOL<title>. It will be display the text THE SCHOOL.
Creating a Paragraph; The <p></p> is use to define the paragraph. HTML automatically
adds an extra blank line before and after a paragraph.
Inserting Line Breaks:The <b> is line break tag. it is use to end a line wherever it is placed
and it does not have a closing tag.
Inserting Space: Space is needed in HTML documents. if a user insert more than one space
anywhere in the document, the browser will show only one space, rest of the space will be
truncated, if user wants to have many space in a HTML document then the   character
entity must be used.
Adding headings /Sub-Heading: There are 6 tags, <h1></h1> to <h6></h6>. the<h1> is
used to specify the largest heading and <h6 > tags specify the smallest.
Q: Describe text formatting tags and their use?
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.111
TEXT FORMATTING TAGS AND THEIR USE:TAXT FORMATTING tags are used to
format the text. some commonly used text formatting tags are described below.
Bold <b></b>: These tags use to make the text bold in a web browser.
Example: <b> NEWS</b>.
Result:It will show as NEWS.
Underline <u></u>: These are used to underline the text with in a tags.
Example: <u>important </u>
Result: important
Italic <I></i>: These tags are used to make the text italic
Example: <i> Pakistan Zindabad </i>
Result: Pakistan Zindabad.
Superscript <sup></sup>: It is use to give the power to the integer.
Example: X<sup>y</sup>
Result: XY
Subscript <sub></sub>: Use to convert the text to subscript that with in the tags.
Example: x<sub>7</sub>
Result: X5
Font size <font size=? ></font>: These tags are used to change the size of the text. Replace
the symbol of? with the range of to 7. Is small and 7 is largest font size.
Example: <font size =4>THE EXAM</font>
Result: THE EXAM.
FONT color<font color =?></font>: These are used to change the color of the font.
Replace the symbol of? By blue, black, yellow, pink etc.
Example: <font color =black > CLASS ROOM </font>.
Result: CLASS ROOM.
Font face <font face=?></font>: These tags are used to change the font face of text that is
within the tags. replace? Symbol by aril, courier, caliber etc.
Example: <font face =French script >the best teacher </font>
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.112
NOTE:
for creating list see pg211
to217.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.113
Adding image in web page: The image tags, <img> with src attribute is use to displaying
image in a web pages. IMAGE is not a part of HTML it is saved in computer
memory.
<img> tags: HTML can insert image in a web page. it has an attributes but not closing tags.
Src attribute: It stands for source. it tells the browser where the image is located. it has
many attributes but src is most important one.
Adding border in an image: Border is the box of line around the image boundary .it can be
set around an image by using its border attribute in the <img> tag its attribute is as following
<img src =” image jpg” border=4>
The no 4 specifies the width of the border in pixels. The browser will display the image with
border.
Specifying image size in a webpage: User can resize the image in a web page by using
height, width, alt attributes in the<img>tag.
Height attribute:
It is use to inserted in the <img> tag to specify the height of the image in pixels. this can be
different then the real. the browser will resize the pic as it is specified.
Width attribute: It is use to inserted the width of the image in <img> Tag. it can be
different from the real [Link] browser will resize the image as specified.
Alt attribute: If for some reasons the browser Can be displayed the image. the user can
insert the alt attribute in the <img> tag to tell reader what image is missing in the
page. the value of the alt attribute provides alternative text in place of image in web
page.
Q:How background and foreground color can be added to webpage:
Applying background and foreground color: To apply a background in a page, insert
bgcolor attribute and for foreground insert text in the<body> tag.
Applying background image: An image can be set as a background of the page by using
background attribute in the <body> tag
<body background=” [Link]”>
If the image is smaller than the web page than it will be repeated till it doesn’t cover the
page.
What is meant by hyperlink which tags are used for hyperlink in html?
HYPERLINKS: The entire WWW is connected by hyperlinks. a web page contains many
hyperlinks and each sends the user to a related webpage or picture/file.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.114
Hyperlink: A hyperlink is a text or image in a web page that links it to other webpage or
another section.
Explanation: It creates a link between text or image. it usually in blue color and underline.
When the reader moves the pointer over any hyperlink the pointer changes to a small hand.
clicking on it allows the reader to jump from page to page.
Anchor tag:
The <a></a> tag is known as anchor. It is used to create a hyperlink which
may be text or image, with the href attribute.
Creating a hyperlink to a webpage:
Anchor with a name attribute is used to create a hyperlink within a web page.
Steps:
It involves two steps to creating the anchor itself and then creating a hyperlink to the anchor
Creating the anchor: create the label in the anchor tag at all location in the web page where
you want hyperlink.
Syntax: <a href =#” label”> text to be displayed</a>
Note:
The# symbol must be put before the label,
Creating a graphical hyperlink:
To create a graphical in web page the syntax should be followed
<a href =”url” ><img src=”name of image file></a>
Example:
<a href =” http:[Link]><img src=” image .jpg”></a>
It will display the [Link] file browser window. When the readers move
the mouse pointer over the image, it will change to small hand and clicking on the image will
open the Federal Board website.
Q:Describe the method by which a table can be created at webpage using HTML.
CREATING TABLES:
Tables are also used in web page to present info in an organized member which
is easy to read and understand by the reader or user.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.115
A table consist of row and columns. some tags are used to create a table in browser which
are as following.
<table></table>:
These tags are use to create a table.
<tr></tr>: these tags define a row.
<td></td>: it defines column. column of every row is called Data cell and it can contain
image text, paragraph. Etc…
<th></th>: it is use for table heading.
Cells spacing: it is use to make space between the cells.
cell padding : it is use to make space between data and border.
Attributes use in creating table:
Following are the attributes use to create table.
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.116
Frames: it is a small rectangular window or pane within a browser window. It also allows
user to display more than one HTML document in a one or same browser window. Each
farm is independent of the other. The <frame> tag is use to place HTML document in a
frame.
Frameset: it defines how to divide the browser window into frames.
the<frameset></frameset> tag are used to achieve this. The user can divide the browser
window into vertical or horizontal frame
Creating a frameset:
The <frameset></frameset> and <farm> tags are use together to create farms in
browser window.
<frameset></frameset>: this tag defines either the series of row or columns.
Two attributes: it has two attributes.
1-cols attribute: it defines series of columns.
2-Rows attribute: it defines the series of rows.
Syntax: both have same syntax
<frameset row=?>
<frameset cols=?>
Where the symbol? Is replace by 30%, 40%, etc.
Example:
For columns: <frameset cols = 50%,70%>
The tag will divide the browser into 2 columns the first one will be of 50% of width of
browser window and the second one will be of 70%. It will create 2 vertical frames.
For rows: similarly <frameset rows=50%,70%>
The tag will be divide the browser window into two equal – size rows that is the browser
window will divide into equal- size horizontal frames.
Exercise
Q1 select the most appropriate answer.
1 internet address which identifies a website is called
URL
2 A collection of webpages hosted on a web server is called?
Website
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad
PAGE#.117
1 <p></p>
2 <h1><h2><h3>….<h6>
3 <b></b>
4 <u></u>
5 <i></i>
6 <center></center>
7 <sup></sup>
8 <sub></sub>
9 <font size=?></font>, <font color=?></font>,<font face=?></font>,
Thanks….
Reviewed by Sir Haris (MS/MPHILL Computer Science)
work as a lecturer at The Peace College Abbottabad
Reviewed by Hafiz Haris MS/MPhil CS, LECTURER at THE PEACE GROUP OF SCHOOLS AND
COLLEGES Abbottabad