[go: up one dir, main page]

0% found this document useful (0 votes)
8 views43 pages

10th Class Comp SC Notes

Uploaded by

luqman.khan229
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)
8 views43 pages

10th Class Comp SC Notes

Uploaded by

luqman.khan229
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/ 43

COMPUTER SCIENCE

NOTES FOR
CLASS 10th

New Edition: 2022

1
CHAPTER PAGE

Chapter 1 3
Programing
techniques

Chapter 2 10
Programming in
C

Chapter 3 Input 17
and Out put
handling

Chapter 4 26
Control Structure

UNIT #5 30
LOOP
STUCTURE

Chapter No.6 34
Logic Gates

Chapter 7: 36
World
wide Web

2
Chapter 1 Programing techniques
Q.No.2 Give short answers to the following questions
i: Define problem analysis?
Ans: Computer is not as much intelligent that it can solve the problem itself.
It is the human, The programmer who writes instruction to solve a particular
problem. To analysis the problem a programmer must know the causes. i.e.
what is the problem about?
How frequently it occurs?
When and where does it occurs?
Who is effected?
ii: How the solution of the problem is planned?
Ans: After analysis the problem is planned. There may be various solution for
a problem but the most effective solution will be cost effective, speedy and
less complex.
iii: Define candid solution of a problem?
Ans: There may be several alternative solution of a problem but the best
solution will be the candid solution of a problem. Selecting and adopting the
best possible solution is called candid solution of the problem.
iv: Define any three problem solving techniques?
Ans: (1)Analogy: Using a solution that solved an analogous problem.

(2) Divide and conquer: Breaking down a large, complex problem for which
solutions exist
(3) Trial-and-error: Testing possible solution until.
v: List various factor are importance for selecting the best solution of any
problem.
Ans: Following factors are importance for selecting of best solution of
problem.
1) Speed: To check whether the solution is the best one for its uses, Speed
is the major measure if selecting the best solution.
2) Cost: It includes space and other hardware and software environment.
A solution is said to be the best solution if it produces optimal desired result
with minimum cost.

3
3) Complicity: It means resources “time and space” required by the
solution of a problem.
Q3: Define an algorithm explain the role of algorithm in problem solving:
Ans: Algorithem: An algorithm is a well defined set of steps to solve particular
problem. It is a written in simple English language. It is used to solve daily life
problems for computer. It is rough work or plan for solving a problem.
Role of Algorithm: An algorithm makes easier to solve complex problem by
dividing it into series of steps e.g. to calculate the average of 20 number,
algorithm will be written as follows.
step1: Read 20 number
step2: Add these numbers
step3: Calculate average
step4: Print average
step5: Exit

Q4: Describe the criteria for measuring efficiency of an algorithm of the


basis of Inputs needed, Processing to be Completed: Decision to be taken
outputs to provided.
Ans: (a) Input needed:

Input is necessary for measuring efficiency of an algorithm. Algorithm


may take different time for different input or different time for same input e.g.
algorithm for multiplication of large matrices will take more time than small
one.
(b) Processing to be completed:

It is another measure for efficiency of an algorithm. It is composed of


wait time, processing time and cycle time.
(c) Decision to be taken:

Decision making is very much important in problem solving. It is used to


get optimal result while used to improve efficiency of an algorithm.
(d) Output to be provided:

Output is very important factor in solving problem. It is the correct


result which shows efficiency of an algorithm. For every input instance an
algorithm should provide correct output.
Q5: What is flowchart? Explain flowchart symbols in detail?
Ans: Flow chart is diagrammatical representation of an algorithm. Here
symbols are used for representing the steps used in an algorithm as it makes

4
the logic clear. The step are lined with the help of arrow lines which show
relation and flow of steps.
Symbols used in flowchart.
1) OVAL: It is used to show the start and end of a flowchart.

2) Parallelogram: It is used to show the input and output in a flowchart.

3) Process box: It is also called rectangle show the process in a flowchart. Here in
process box formula or expression is written or a variables is initialized.

4) Decision symbol: It is diamond shape used to show decision in a flowchart. It has


two possible direction i.e. YES or NO (TRUE or FALSE)

5) Arrow: These are used to link different steps in flow chart and show the flow of
steps.

5
Example Flow Chart No 8

START

Input N, E

P=1 , K=1

P=P*N , K=K+1

IS K≤E YES

NO

Output P

STOP

6
Q6: Write an algorithm to calculate the factorial of given number and
draw flowchart for it.
Ans: Algorithm:
Step1: Read the number N.
Step2: Set factorial and K: F
F=1 and K=1
Step3: Repeat step 3 through step 5 until K<=N
Step4: F=F*N
Step5: K=K+1

START

Input N

F=N , K=1

F=F*K , K=K+1

IS K<N YES

NO

Output F

STOP

7
Step6: Print Factorial (F)

Q7: Write an algorithm and draw a flow chart to resolve quadratic


equation.
Ans: Step1: Read the value a,b,c
−𝒃+√𝒃𝟐−𝟒𝒂𝒄
Step2: Calculation the discriminant 𝒙𝟏 = 𝟐𝒂
−𝒃−√𝒃𝟐−𝟒𝒂𝒄
𝒙𝟐 =
𝟐𝒂

START

Input a,b,c

−𝑏 + √𝑏2 − 4𝑎𝑐
𝑥1 =
2𝑎

−𝑏 − √𝑏2 − 4𝑎𝑐
𝑥2 =
2𝑎

Output x1, x2

STOP

Step3: Print x1 and x2


Step4: Exit

8
Q8: Write an algorithm and draw a flow chart to find average marks of
eight subjects for a student?
Ans: Algorithm:
Step1: Read the marks of eight subjects Step2: calculate the sum of eight subjects
Step3: Avg=sum/ 8
Step4: print Avg
Step5: stop

Flowchart
Start

Enter marks of subjects

Avg=sum/subjects

Display Average

Stop

9
Chapter 2 Programming in C
Q2: Give short answers to the following questions:
I. Different between program syntax and programming semantic.
Ans:

Program Syntax Program Sementic


Syntax mean the rule of any It describe the meanings of a
programming language. The statement. i.e how the statement
statements are written according to will be executed e.g. product=x*y; //
the syntax for example: Syntax of Here semantic to that first x will be
writing programming statement in multiplied with y then the value will
“C” is as printf(“escape be assigned to variable product.
sequence/format specifier /string”,
variable/expression/ constant);

ii. Different between Low Level Language (LLL) and High Level Language
(HLL)?
Ans:

Low Level Language High Level Language


i) Low Level language are close to i) High level language are close to
machine language of “0” and “1” human language i.e. English
ii) Programs written in low level ii) Programs written in high level
language are difficult to develop, learn language are easy to write change or
and debug debug
iii) Example of low level language are iii) Example of high level language are
machine language and assembly C++, JAVA, Pascal, Fortran etc.
language.

iii What is IDE?


Ans: IDE means integrated development environment. It is also called
programming environment. It is an application program in which we can
develop, edit, run and debug.
IDE provides GUI (Graphical User Interface) due to which the computer
programming has become an easy task.
iv. What is OOP?
Ans: OOP means Object Oriented Programming. An object is a component of
a programming that has modules and data structures. An object is designed

10
once and can be used again and again. It is a modern and easy way to write a
program. Example are C++ and JAVA.
The programming technique in which objects and classes are used is called object oriented
programing.

v. What are characteristics of HIGH LEVEL LANGUAGE?


Ans: High level languages are closer to human language. High level language
are easy to learn. These are easy to understand. It is easy to write program. In
high level languages. It is easy to detect errors and remove in high level
languages. Built in function are used in high level languages. The programs
written in high level languages are machine independent.
vi. Different between compiler and Interpreter?
Ans:

Compiler Interpreter
It is a translation software used to translate It is also a translation software used to
high level language source program into translate high level language program into
machine language object program. It machine language but line by line. Example:
translates the over all program at one time. Basic, Visual Basic
Examples of languages using compiler are C,
C++ etc

vii. What is Header File?


Ans: It is a collection of standard library functions to perform different task is
called header file.Header file is the part of C programming. Many header file
can be included in a single C program. The extension of header file .h #include
preprocessor directive is used to include header files in program. E.g.
#include<stdio.h>
#include<conio.h>

viii. Different between source program and object program?


Ans:

Source Program Object Program


The program written in high level language The program when converted to machine
is called source program. language is called an object program.
Source program in C-Language is saved with The object program is saved with an
an extension (.c) e.g.(first.c) extention (.obj) e.g.(first.obj)

11
ix. What are reserved words?
Ans: These are also called keywords. They have special meaning to compiler.
e.g. int, float, if, else, switch are reserved words. If we write int as INT, it will
generate an error other keywords are given as under case, include, getch,
clrscr, while, continue, do, break etc.
So reserved word is defined as “The word which are already created and
defined inside a language translator are called reserved words.
Reserved words can not be used as variable names.
x. writer rules for variable name ?

Ans: Rules for variable names are following:

i. Variable name is always started with an alphabets and underscore.


ii. Blankspaces are not allowed
iii. No reserved words can be used as variable name.
iv. No special symbol are allowed other than underscore.
v. Capital and Small letters have different meaning for compiler.
vi. Length of variable name depends upon compiler.
vii. The number may be used but not at the start of variable name.

Xi WHAT IS PURPOSE OF CONSTANT QUALIFIER?

Ans: Constant qualifier is used to define constant value. It is used with


preprocessor directives. const is a reserved word.
Syntax: Constant data type identifier = value;
e.g. Const int abc=50;
or
#define abc 50
Q3. What is programming language? Explain different types of low level
languages.
Ans: Programming language is also called computer programming languages.
When we want to solve a daily life problem with the help of computer, here
we have to write computer program. For these purpose we need programming
language. There are two(2) main categories of programming language.

12
1. LOW LEVEL LANGUAGE
2. HIGH LEVEL LANGUAGE
There are two types of Low Level Language.
i. Machine Language
ii. Assembly Language

MACHINE LANGUAGE: Machine language is also called binary language. Binary


language is composed of 0 and 1, the binary codes. It is also called native
language of computer. Computer can directly understand the machine codes.
The programs are difficult to write learn and debug in machine language.
ASSEMBLY LANGUAGE: It is also called a low level language in which English words
i.e. commands are used. There are called mnemonics. To translate assembly
language program a program called assembler is used.
Q4: What are HIGH LEVEL LANGUAGE? Explain any five types of HIGH LEVEL
LANGUAGE.
Ans: High level language are closer to human languages.

Eg English language, programs written in high level languages are easy to


understand, develop and debug. Few common examples of high level language
are.
i. C Language: C language was developed by Dennis M. Ritchie of Bell
laborites in 1972. C language is procedural programming languages
with low level features.
e.g ability to handle memory addresses and bits. The operating
system UNIX was written in C language.
ii. C++ Language: It was developed by Bjarne stroustrup in 1980s. It is an
(OOP) language. It is an extension of C language it includes all the
elements of C language with other features like objects, classes,
events and object-oriented concept. Word processors and
spreadsheet programs are written in C++.
iii. Visual Programming Language: These languages are used for
developing GUI applications. These are fifth generation languages
and object oriented programming (OOP) language example are visual
basic and visual C++.
iv. C# Language: It is an object oriented language. It was developed by
Microsoft Corporation in 2000. It is based on C++ with element from
visual Basic and JAVA. It is used to write web application, games
utilities, operating system Compiler can be developed in C#.

13
v. JAVA Language: JAVA is an object oriented programming language
developed by Sum microsystem in early 1991. It is very similar to C++
language used to design web pages, JAVA applet is as JAVA program
that is run inside another program e.g. web browser. applets are used
to rotate languages for animation and used to access online data
bases.

Q5: What is the basic structure of C Program? Also explain different types of
preprocessor directives?

Ans: Basic Structure of C-Program: C program is consist of


1) Preprocessor directives
2) main() function
3) Body of main function
1) Preprocessor Directive: Instructions given to the compiler before beginning
of actual program. These are called preprocessor directives. These are
also called compiler directives. Preprocessor directives normally starts
with # and the keyword include or #define
2) main() function: main() function must be included in every C program when
C program is executed the control directly goes to the main function .
if the main() function is not included in the program the error message
generated.
Syntax: main()
{
Program statement(s)
}
Body of main(): Statement written inside the main. These statements make the body
of main program.
Syntax: main()
{
Statement 1;
Statement 2;
Statement 3;
:
:
Statement n;
}
Each statement in C ends with semicolon(;).

14
Q6: WHAT ARE CONSTANT AND VARIABLES? DIFFERENT THEM WITH EXAMPLE.

Ans:
Constant:
i. Constant is a quantity which does not change during execution of program.
ii. Constant is the value that is stored in location i.e. variable.
iii. Constant are of two main types. (1) Numeric Constant (2) Character Constant
iv. Example of numeric constant are 2,3,5,65.
v. Example of character constant are ‘a’, ‘t’, ‘I’, ‘q’, “Tank” etc
Variable:
i. Variable is a quantity whose value many changes during execution of a program.
ii. A variable represent the memory location in the computer memory.
iii. There are two main types of variable (1) Numeric Variable (2) Character Variable
iv. Example of Numeric variables are Roll No., Marks, Percentage.
v. Example of Character variable are name, father name, address.Q7:
Explain different data types used in C Language with example.
Ans: There are three(3) main data types in C Language
i) int
ii) float
iii) char
int (int, long):

Int stands for integer having no decimal point. It takes two bytes
in computer memory range of int is -32768 to +32767.Long is another use of
integer data having range of -2,147,480,648 to +2,147,480,647. It takes a bytes
in computer memory syntax int variable.
Float:

It has three(3) types float, double and long double. It represent


real number with the precision of 7 digit. Float takes 4 bytes and its range is
3.4x10-38 to +3.4x1038.
Double takes 8 bytes in memory its range is 1.7x10-308 to +1.7x10308 long
double takes 10 bytes in computer memory and its range of value is 3.4x10-
4932 to 3.4x104932.
Char:

Char is the type character data. Character takes 1 byte in


computer memory. Numbers, alphabets and symbols show character
individually.

15
Syntax: char ch;
Q8: What is type casting? Explain with example.
Ans: The casting is the process of converting one data type into another.
There are two types of type casting.
1) Implicit Type Casting
2) Explicit Type Casting
Implicit type casting is done by compiler automatically e.g. If a user declare a
statement as c=a+b; where the type of a is int where b is long. Compiler will
automatically convert type int to long.
Explicit type casting is done on the part of programmer e.g. if user declares a
statement as n=10.1; and y=5.3; then n%y shows an error massage. Then
programmer uses cast operator to convert. The type float into int as follows
Int (n) % int(y)
‘n’ will be converted to 10 and y to 5 and the result will be 0;
Q9: How variables are declared and initialized in C language give example.
Ans: Variable Declaration:

Assigning a name and data type to a variable is called variable declaration.


Syntax:

Data type list of variable separated by commas.


For Example:
Int a,b,c;
float d,e,g;
Note:
Variables of a same data type can be declared in a single line or on more than
one lines.
Variable Initialization:

Assigning a known value to a variable is called variable


initialization.
Syntax:
Data type variable=value;
e.g. int a=3;
float average=3.5;
16
Chapter 3 Input and Out put handling
Q.2# Give short answers to the following questions:-
i. What is the use of format specifier? Give examples.
Ans: Format Specifier:
Format specifier is a conversion specification code used according to the
data type of variable used in input and output function. Format specifier
consists of one or more alphabets with % sign. e.g. %d, %c, %s.
The most common format specifiers are
%d : used with integers
%f : used with real numbers
%c : used with characters
%s : used with strings
ii. Why escape sequences are used? Give example.
Ans: Escape Sequence:
These are non printed character used to format output.
These are the combination of an alphabet and a symbol \. These can be used
everywhere in the string.
The most commonly used escape sequences in C language are following.
\n is used for new line
\t is used for tab
\a is used for beep sound
\b is used to delete one character backward etc.
iii. What is the purpose of gets() function?
Ans: Purpose of gets() function:

gets() function is used to read a string from keyboard. Difference


between gets() and scanf() function is that scanf() function does not read the
blank space while the gets() allows the blank space used in string message. No
format specifiers are used in gets() function.
Syntax:
gets(string);

17
Where string is a string constant or variable

iv. Different between getch() and getche() function


Ans: getch() function:
getch() function is used to read a signal character without an echo.
getch() function is input function.
getche() function:
getche() function is an input function which reads a character with an
echo. getche() function displays character on screen.

Q5: Evaluate the following equations.


i) 9-5*(6+2)
9-5*8
9-40
-31 Answer
ii) 60/10*24+3
6*24+3
144+3
147 Answer
iii) 100%50-100%3
0-100%3
0-1
-1

iv. Different between simple and compound assignment operator.

Simple Assignment Operator Compound Assignment Operator


Simple assignment operator is used to Compound Assignment operator is the
assign values to left side variable: combination of an assignment operator and
Syntax variable = variable / constant / expression an arithmetic operator.e.g “+=”, “-=”, “*=”,
e.g int a=2; Here the value ‘2’ is assigned to “/=”, “%=” are compound assignment
variable ‘a’. operators. Where a+=2; means a=a+2; and
Here on right side there may be the so on
variable , constant or an expression. Compound assignment operator first
performs mathematical operation and then
assign value to variable.

LONG QUESTIONs
Q3: What are operators? Explain different types of operators with
examples?

18
Ans: Operator:
Operators are special symbols used to perform an operation
on operands for example x+y is an expression, where x and y are operands and
+ is an operator.
Types of operator:

i. Arithmetic Operator:
These operator perform arithmetic operation like +, -, *, /, %.
Here:
OPERATION OPERATOR DESCRIPTION
Addition + Adds the values
Subtraction - Subtract one value from other value
Multiplication * Multiply two values
Division / Divide one value by another value and returns the quotient
Modulus % Modulus is used for remainder division

ii. Relational Operator:


These operator are used to check the condition in a program. Result of
relational operators is either true or false. >, <, >=, <=, ==, != are relational operators.
e.g.
int a,b;
a=3;b=5;
if(a==b)
printf(“true”);
else printf(“false”);

iii. Assignment Operator:


Discussed earlier….in Q6: Different between simple and compound assignment operator.

iv. Compound Assignment Operator:


Discussed earlier….in Q6: Different between simple and compound assignment
operator.

v. Logical Operator:
These operators are used to evaluate compound condition or relational operators.
&&, ||, ! are logical operators:
Example:
if((a>b)&&(a<c))
The result will be true if both the conditions are true.
If((a>b)||(a>c))
The result will be false if both the condition will false.
if!(a>b)
The result will be true if condition will be false and false if condition will true.
vi. Increment Operator:++

19
Increment Operator is used to add 1 to the value of variable. There are two modes in
which increment operator is used.
a. Prefix Increment Operator: Prefix increment operator is used to add 1 to
the value of a variable before it is used in an expression.
b. Postfix Increment Operator: Postfix increment operator is used to add 1 to
the value of variable after it is used in an expression
vii. Decrement Operator:
The decrement operator is represented by a double minus (--) sign. It is used to
subtract 1 from the value of an integer variable. The decrement statement is written
as.
x--; OR --x;
a. Prefix Decrement Operator: When a decrement operator is used in prefix
mode in an expression, it subtracts 1 from the value of the variable before
the value of the variable is used in an expression.Sum = a+b+--c;
b. Postfix Decrement Operator:When a decrement operator is used in postfix
mode in an expression, it subtracts 1 from the variable after the value of the
variable has been used in an expression.
For Example: Sum = a+b+c--;
viii. Conditional Operator:
It is an alternate of if-else structure. It is the combination of ? and :
It is also called Ternary Operator.

c. Syntax:
(condition) ? true-case : false-case;
Example: (a>b) ? printf(“a is greater”) : printf(“b is greater”);

Q4: What is an output function? Describe in detail different types of output


functions.
Ans: Output Function: These functions are used to fetch values of
variable (from memory) and send to output device. i.e. monitors. C-
language offers following output functions.
d. Such as: (i) printf() (ii) puts() (iii) cout<<
i. Printf() function:
It is an output function used to print strings, values of variables and
result of arithmetic expression.

Syntax;
printf(“escape_sequence/format_specifier/strings”,variables);

OR

Printf (“\n %d “,sum);

20
Example: printf(“I love programming”);
printf(“The result is %d”,sum);
printf(“product of 2 numbers = %d”,a*b);
ii. Puts() function:
Puts() function is used to print a string on the screen.
Syntax: puts(string);//where string may be a string constant or string variable.
Example: puts(“My passion is programming”);
NOTE: <string.h> is the header file used with puts() function
iii. Cout<<:
It is an object of C++. It is an output function used to print string, variableor
expression on default output device (monitor). It is used with insertion operators
<<
Syntax: cout<<variable<<”string”<<expression;
Example: cout<<”I love Pakistan”;
Cout<<”The sum of a and b =”<<a+b;

Q.5: Discuss different format specifiers and escape sequences used in


prinft() function.
Ans: Format Specifier:

Format specifier is a conversion specification code used according to the


data type of variable used in input and output function. Format specifier
consists of one or more alphabets with % sign. e.g. %d, %c, %s.
The most common format specifiers are
%d : used with integers
%f : used with real numbers
%c : used with characters
%s : used with strings

Ans: Escape Sequence:

These are non printed character used to format output.


These are the combination of an alphabet and a symbol \. These can be used
everywhere in the string.
The most commonly used escape sequences in C language are following.
\n is used for new line
\t is used for tab
\a is used for beep sound
\b is used to delete one character backward etc.
21
Q6: Explain different function of input function with example?
Ans: Input functions are used to get value from user and assign it to variable.
Types:

I) scanf()
It is an input function of C language used to read value of
variable. i.e. numeric or character from keyboard and assign it to
variable. This function waits till the user input value from keyboard.
Syntax:
Scanf(“format_specifier”,&variable1,&variable2…….&variablen);
Example:
Scanf(“%d%d”,&a,&b);
II) getchar()
It is an input function used to read a single character from keyboard
with echo. It needs an enter after inputting a character.
Syntax:
getchar();
III) cin>>
++
It is an input function of C language used to take the value of
variable. It is also aC++ object.It is used with extraction operators >>
Syntax:
Cin>>var1>>var2>>var3;
Example:
Cin>>a>>length>>width;
Q7: Write a program that reads three number and prints their sum,
product and average.
Ans:
#include<stdio.h>
#include<conio.h>
void main()

{
clrscr();
int a,b,c,sum,pro;
float avg;

22
printf(“Enter value of a,b and c \n”);
scanf(“%d %d %d”,&a,&b,&c);
sum=a+b+c;
pro=a*b*c;
avg=sum/3;
printf(“The sum = %d \n”,sum);
printf(“The product = %d \n”,pro);
printf(“The average = %f \n”,avg);
getch();

Q8: Write a program that reads the base and height of a triangle and prints
its area.
Ans #include<stdio.h>
#include<conio.h>
void mani()

{
Float b,h,area;
clrscr();
printf(“Enter the base and height of triangle \n”);
scanf(“&f %f” ,&b,&h);

area=(b*h)/2;
printf(“The area of the triangle is %f”,area);
getch();
}

Q9: Write a program that reads temperature in Celsius convert it into


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

23
float fah,cal;
clrscr();

printf(“Enter the Temperature in Celsius \n”);


scanf(“%f”,&cal);
fah=(9.5/5.0)*cal+32;
printf(“The Temperature in Fahrenheit is %f”,fah);
getch();
}
Sample Output
Enter the Temperature in Celsius
15
The Temperature in Fahrenheit is 59.0000

Q10: Write a program that reads name and address of a student and prints it
on the screen using gets() and puts() function.
Ans: #include<stdio.h>
#inlcude<conio.h>
#inlcude<string.h>
void main()

{
char name[20],address[30];
printf(“Enter your name please: \t”);
gets(name);

printf(“Enter your address: \t);


gets(address);
puts(name);
puts(address);
getch();

24
Sample OUTPUT
Enter your name please Muhammad
Enter your address Dera Ismail Khan
Muhammad

Dera Ismail Khan

25
Chapter 4 Control Structure
Q2: Write a C-Program to input three integers. Find out the largest among
these integer using if-else structure and print on the screen.
Ans: #include<stdio.h>
#include<conio.h>
void main()

{
int x,y,z;

printf(“Enter any three numbers \n”);


scanf(“%d %d %d”, &a,&b,&c);
if(a>b && a>c)
printf(“%d is greatest number”,a);
else
if(b>a && b>c)
printf(“%d is greatest number”,b);
else

printf(“%d is greatest number”,c);


getch();
}
SAMPLE OUTPUT

Enter any three numbers


5
9

1
The largest value is 9.

26
Q3: Write a program in C to input a single character and print a massage “it
is a vowel” or “it is a consonant”. Using if-else statement.
Ans: #include<stdio.h>
#inlcude<conio.h>
void main()

{
char ch;
clrscr();
printf(“Enter any character \n”);
scanf(“%c”,&ch);
if(ch==’a’||ch==’A’|| ch==’e’||ch==’E’|| ch==’i’||ch==’I’|| ch==’o’||ch==’O’||
ch==’u’||ch==’U’)

printf(“The character %c is a vowel character”,ch);


else
printf(“The character %c is a consonant”,ch);
getch();
}
SAMPLE OUTPUT
Enter any character
E

The character E is a vowel character

Q4: Write a program in Q3 using switch-case structure.


Ans: #include<stdio.h>
#include<conio.h>
void main()

{
char ch;
printf(“Enter any character \n”);
scanf(“%c”,&ch);
printf(“Enter any character \n”);

27
switch(ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’:

printf(“The character %c is a vowel”,ch);


break;
default:
printf(“The character %c is a consonant”,ch);
}

getch();
}
SAMPLE OUTPUT
Enter any character
M
The character M is a consonant

Q5: Write the output of the following program?


#include<stdio.h>
void main()
{
int a,b,c;
a=b=10;

28
c=13
if((a==b)&&(a>c))
printf(“Condition is True \n”);
else
{
printf(“a is equal to b \n”);
printf(“a is not greater than c \n”);
}
}
Ans: OUTPUT

a is equal to b

a is not greater than c


Q6: Find out error if any in the following program?
#include<stdio.h>
void main()
{
int a;
irintf(“Enter any number: “);
scanf(“%f”,&a);
if(a>10)
printf(“value is greater than 10”);
else if
printf(“value is less than 10”);
}
ERRORS:
1. The variable ‘a’ is declared as integer but it is read in scanf() function as float
2. After ‘if’ condition, ‘else if’ is used instead of only ‘else’ which is against the syntax.

29
UNIT #5 LOOP STUCTURE
Q2: Explain different types of looping structures with example.
Ans: There are three types of looping statements in C-Language
1. For LOOP
2. While Loop
3. Do-While Loop

For Loop:
It is a type of loop structure which is used to repeat a statement or a set
of statement for known number of times. It is used in a situation where we
already know how many time we want to repeat a statement or a set of
statements.
Syntax:
For(initialization; condition; increment/decrement)
{
Body of the loop;
}
Example:
#include<stdio.h>
#include<conio.h> OUTPUT
void main()
1 2 3 4 5 6 7 8 9 10
{
int x;
clrscr();
for(x=1;x<=10;x++)
printf(“%d \t”,x);
getch();
}
While Loop
It is a type of loop used to repeat statement(s) for a number of times
until the given condition remains true. It is used in a situation when we don’t
know in advance how many times the loop will be executed.
Syntax:
While(condition)
{
Body of the while;
}
30
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();

n=1; OUTPUT

Pakistan
while(n<=5) Pakistan
{ Pakistan
Pakistan
printf(“Pakistan \n”); Pakistan
n++;
}
getch();
}

Do While Loop:
Do-while loop is used to repeat a statement or set of statements until
condition remains true. In this loop body of the loop occurs before condition.
So body of the loop executed one time even if the condition becomes false at
the very first time.
Syntax:

do
{
statement(s);
increment/decrement;
}
while(condition);
Example:
a=0;
do
{
printf(“%d”,a);
a=a+1;
}
while(a<0);

31
Q3: Differentiate between the do-while and while loops. Explain with
program example.
Perimeter while loop do-while loop
Checking It first needs to check the The execution of
of condition, and only then can we statement(s) occurs at least
Condition execute the statement(s). once. After that, the
checking of the condition
occurs.
Semicolon while(condition) while(condition);
No semicolon is present at the A semicolon is present at the
end ofthe while loop. end of the do-while loop.

Requirement of We don’t require any brackets if We would always require


Brackets there brackets
is only a single statement. in this case.
Initialization We always need to initialize a The initialization of the
of Variables variable in a condition before the variable may occur within or
execution of before the
this loop. execution of the loop
Controlling The while loop is an entry-controlled The do-while loop is an exit-
type of loop. controlled type of loop.
While Loop
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1; OUTPUT
Pakistan
while(n<=5) Pakistan
{ Pakistan
Pakistan
printf(“Pakistan \n”); Pakistan
n++;
}
getch();
}

Do While Loop:
Example:
a=0;
32
do
{
printf(“%d”,a);
a=a+1;
}
while(a<0);

Q4: write a program in C to calculate and print the product of the even
numbers from 1 to 100 by using the while loop.
Ans : #include<stdio.h>
#include<conio.h>
void main()
{
int n, pro;
Pro=1;
While(n<=100)
{
//Here is the condition to check Even number
if(n%2 == 0)
printf(" %d ",n);

// increasing loop counter by 1


n++;

pro=pro*n;
}
Printf(“\n Product = %d” ,pro);
getch();
}

33
Chapter No.6
Logic Gates
Q2. How data is represented in computer?
Ans : Data is represented in computer in the form of binary digits 0 and 1 .
Computer can only understand the language of 0 and 1. When we enter data in any of the
form i.e text , pictures, audio or video all the data is converted into machine language
before processing by the CPU.
eg when we type A it is converted into binary form 100001.

Q.N3. What are the three basic logic gates Describe in detail with Function and diagram
and Truth table ?

Ans There are three basic gates These are


1) And Gate : 2) OR gate 3) Not gate
1) And Gate : It is that type of logic gate which performs logical multiplication on
input signals . It takes two or more input signals and produces only one out put
signal. Output will be 1 if all the input are 1 otherwise output will be 0 if any of
the input is 0.
It can be represented mathematically as
F= a.b ( For two input signals)
Input Output
A B X=a.b
0 0 0
0 1 0
1 0 0
1 1 1

Diagram a
B F=a.b
2) OR Gate : It is that type of logic gate which performs logical addition on input
signals . It takes two or more input signals and produces only one output signal.
Output will be 0 if all the inputs are 0 otherwise output will be 1 if any of the
input is 1.
It can be represented mathematically as
F= a+b ( For two input signals)

Input Output
x y F=x+y
0 0 0
0 1 1
1 0 1
1 1 1

Diagram
F=x+y
34
Not gate : It is also called inverter gate it is used to convert high voltage to low
voltage ie if input signal is x then not gate will change it into .
Diagrammatically it is shown as

X O

Input x Output
0 1
1 0

35
Chapter 7: World wide Web
Q: Define www?
Ans: World wide web is an information system on the Internet allow the
document to the connected to other document by hyperlinks, enabling the
user to search information by moving from one document to another
document on the Internet. Created in Hypertext Markup Language, HTML.
HTML is the main language to develop these document for (www).
Q: Define Web Page?
Ans: Web page is a document on Internet that can be accessed through a web browser
every document has its own unique web address called URL.
Web page contains text, image, sound, graphics and video clips all at one place.

Q: Define Website?
Ans: Web site is a collection of related web page, hosted on a web server. It is accessed
through web address or URL e.g. www.khyberphkhtunkhwa.gov.pk

Q: Define Web Browser?


Ans: It is a software that enable a user to retrieve information on web which is accessed
through URL. Web browser allow user to open multiple websites in different tabs of the
same window or in different window.
These browser are easy to use and learn. Two important web browser are

1. Internet Explorer from Microsoft


2. Google Chrome from GOOGLE

Q: Define web server?


Ans: Web server is a computer that facilitates and deliver web pages from different
web sites. every web server has an IP (Internet Protocol) address and possibly a domain
name e.g. http://www.pakistan.gob.pk/ Here Pakistan.gov.pk is national symbols. It’s a
web page any computer can be turned into a web server by installing server software and
connecting it to Internet.

Q. Define URL?
Ans. URL means unique address for a file accessible on internet by URL a
web page can be accessed by writing it on address bar of browser general
formal of URL is :Protocol://hostname / other information.
Example: http://www.disep.com.pk /main.
Have http mean hyper text transfer protocol specifies how the information
from the likes is the domain name computer where resource is located.

36
Q . Define Search Engine?
Ans. To find information on internet search engine is used which is a Software. It is an
application which is used help users to find information on web. Finding exact information
on web is not an easy task therefore user just type keyword in search engine it displays a
list of websites with a title and a description user just click on a website that he/she wants
to open.
Example of search engine are Google and yahoo.

Q. 2. What is website? Describe different types of websites with example.

Ans. Website.
A website is a collection of related web pages hosted on a web
server. It is accessible through an Internet address know as URL. For example a websites.
“ WWW. Khyberpakhtunkhwa.gor.pk
There are different types of website on the internet. Each websites is providing
information about different topic, some are used for on net page.

i) Web portals.
These web portals provide different types of services and a point of
access to information on web the services provided on these website are online
shopping , news , stock prices, email and search engine etc, for example a web
portals of a university may contain information about background, admissions,
fee Structure , facilities, curriculum ,, programs etc. www.aiou.edu.pk is web
portal of Allama Iqbal open university
ii) News website.
News websites provide information about current events and
opinions these sites provide allow the user or visitors to provide their views
and feed back about the current affairs, some example of these website are
www.urdupaint.com , e.jang.com.pk , marsitriq.com.pk ETC.
iii) Information websites.
Informational websites provide information on any topic in the form
of text, graphics, sound and videos. sometimes these websites also provide a
facility to edit articles.
iv) Educational website.
This website is created for educational purposes Such websites are organized to
impart knowledge about any topic and to educate people. These website contain animations,
slide presentations , audio video lecture and tutorials about certain topic some examples are
www.educationworld.com , www.ilmkidunya.com and www.knowledgepk.com etc.\
v) Personal website
The website is created and maintained by individuals for their personal use. A
person can Provide some information about this interests, hobbies, quantification,
publication etc. person website individual can share personal information to family and

37
friends on a personal website. These websites are open to all the visitors and people can
access it anywhere any time

vi) Business website.


Business websites provide facilities to maintain business
relationships and settings information, services and commodities via internet
the business revises provided on these websites are sales, purchase order
Booking , invoice etc. These website can also provide their financial services
such as bunking ticket reservation, stocks and purchase etc.
vii) Entertainment websites.
These websites contain the entertainment material that is available
to computer users. These entertainment websites include sports, music, radio,
T.V, Computer games comedy and fashion and movie website.

Q.3. write the title tags for the following?


Ans.
For Tag
i. Paragraph <p></p>
ii. Heading <hi></hi> to<h6>
iii. Bold <b></b>
iv. Underline <u></u>
v. Italic <i></i>
vi. Center <center></center>
vii. Superscript <sup></sup>
viii. Subscript <sub></sub>
ix. Font size, <font size=20, color=red,face=Times New Roman></font
Color, typeface

38
Q.4. Create a web page in HTML that displays image of a printer the Width of
image of should be 250 pined and night is 150 pined.
Ans.
<html>
<head>
<title> image of printer </title>
</head>
<body>
<img src=”printe.jpg” width=250 height= 150 alt =”image of a printer”>
</body>
</html>

Output is
Q.5. Describe how background color and image are applied for web page.
Ans.

Applying Background and foreground colors


To apply a background color in a page, insert the attribute in the <body> tag
following code will do so.
To apply background color
<html>
<head>
<title>background color</title>
</head>
<body bgcolor= “green”>
The color of the body will be green.
</font>
</body>
</html>
…………………………..

To apply image in background


To apply image in the background in a page, insert the image attribute in
the <body> tag, following code will do so.

39
<html>
<head>
<title>background image</title>
</head>
<body background= “image.jpg”>
This image will show in the body of the page

</body>
</html>
…………………………..

Q.6. Create an HTML document that contains a graphical type link.


Ans.

<html>
<head>
<title> link tag</title>
</head>
<body>
<a href= “Http” // www.aust.edu.pk><image src =”logoaust.jpg”></a>
<br>
click on fine image to visit the site of abbottabad university website
</body>
</html>

Q.7. Create a web page in html that contain unordered and ordered lists.
Ans.
<html>
<head>
<title>
Creating unordered list </title>
<head>
<body>
<h3> types of Computer Lagnuages <h3>
<ol>
<li> low level language </li>

40
<ul>
<li> machine language</li>
<li> Assembly language
</ul>
<li> High level language </li>
<ul>
<li> FORTRAN </li>
<li> c/c++</li>
<li> java </li>
</ul>
</ol>
</body>
</html>

Q.8. Create a web page that contain hyperlink to three websites.

<head>
<title>
hyperlink to three websites
</title>
</head>
<body>
<a href= “Http // www.bisep.edu.pk”> BISE Peshawar </a>

<a href= “Http // www.upesh.edu.pk”> University of Peshawar


</a>

<a href= “Http // www.bkuc.edu.pk”> BKU Charsadda </a>

</body>
</html>

Q.9. Create a web page that contain hyperlink to three websites.


>html<
>head<
41
>title> Class Time Table </title<
>head/<
<body>
>"table border="2px" width="50%<
>tr<
>th>DAYS </th<
>th colspan=”5”>periods</th<
>tr/<
<tr>
<td>Monday</td>
<td>Eng</td>
<td>Urdu</td>
<td>CS</td>
<td>Islamiat</td>
<td>CHD</td>
</tr>
<tr>
<td>Tuesday</td>
<td>Eng</td>
<td>Urdu</td>
<td>CS</td>
<td>Islamiat</td>
<td>CHD</td>
</tr>
<tr>
<td>wednesday</td>
<td>Eng</td>

42
<td>Urdu</td>
<td>CS</td>
<td>Islamiat</td>
<td>CHD</td>
</tr>….. and so on
</body>
</html>

The End

43

You might also like