Computer Class 10th Notes 1
Computer Class 10th Notes 1
SCIENCE
TH
CLASS 10
EASY NOTES
PROGRAMMING TECHNIQUES
v. List various factors for selecting the best solution of any problem.
The following factor are very important in selecting the best solution of any problem.
a. Speed
b. Cost
c. Complexity
2
a. Speed
To develop an efficient solution for solving the problems, the time it uses is the
major measure of the efficiency of that solution.
b. Cost
Cost of a solution includes, space and other hardware and software requirements.
The solution will be considered to be the best of if it gives optimal desired result
with minimum cost.
c. Complexity
Complexity of the solution means the resources required by the solution, most
commonly known as time – how many steps it needs and the space – how much
memory it takes.
Algorithm
Algorithm plays and 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
processing into steps will make the problem solution an easy task. “Divide and Rule” a very
famous rule that makes the problem solving an easy task.
For example, consider we find the average of 20 numbers. If this process is defined in small
discrete steps then we can make the solution very easily.
3
Q4. Describe the criteria for measuring efficiency of an algorithm on the basis of:
a. Inputs needed
b. Processing to be completed
c. Decision to be taken
d. Outputs to be provided
Speed or running time – the time it takes for an algorithm to complete, and
‘Space’ the memory used by the algorithm during its operation.
The efficiency of an algorithm can be also measured on the basis of following factors:
a. Inputs needed
The amount of time an algorithm takes depends on how large in the input on which
the algorithm most operate. For example, multiplying huge matrices takes longer than
multiplying small ones, etc. An algorithm might take different amounts of time inputs
of the same size.
b. Processing to be completed
The time required to complete the operation is a measure for efficiency of an
algorithm. The process completion time is composed of cycle time, wait time and
processing time.
c. Decision to be taken
Problem solving and decision-making are important skills to improve the efficiency of
an algorithm and get optimal result.
d. Outputs to be provided
An Algorithm is said to be correct if, for every important instance, it ends with the
correct output.
4
Q5. What is flowchart? Explain flowchart symbols in detail.
Flowchart
Flowchart is a diagrammatic or symbolic representation of algorithm which is used to
analyze, improve, document and manage a process or program. It is a type of diagram
representing a process using different symbols containing information about steps in a
problem. Each of the symbols is linked with arrows to illustrate the flow direction of the
process towards a solution.
Arrow lines used to show the flow of control from one step
to the other. They also indicate progress from one step to
Arrow lines another
5
Q6. Write an algorithm to calculate the factorial of a given number and draw flowchart
for it?
Algorithm:
Start
This algorithm is used to calculate
factorial of a given number.
Step 5. k = k + 1
FR = FR * K
[End of While loop] K= K + 1
Step 6. Write FR
Step 7. Exit
IS
YES
K < = NUM
NO
Output FR
Stop
6
Q6. Write an algorithm and draw a flowchart to resolve quadratic
equation?
Algorithm:
Start
This algorithm is used to resolve the
quadratic equation
Step 1. Read A, B, C
Input A, B, C
Step 2. Set D = SQRT(B x B - 4 x A x C)
Step 3. Set X = – B + D / 2A
Step 4. Write X, Y
Step 5. Exit
X = - B + D / 2A
Y = - B – D / 2A
Output X, Y
Stop
7
Q8. Write an algorithm and draw a flowchart to find average marks of
eight subjects for a student.
Algorithm:
Start
This algorithm is used to find average
marks of eight subjects for a student.
AVG = SUM / 8
Output AVG
Stop
8
UNIT 2
PROGRAMMING IN C
Low level language can directly access computer High level languages cannot directly access the computer hardware
2
hardware and comprised of mnemonics. and it is comprised of functions, modules, classes and GUI etc.
3 Low level languages are machine dependent. High level languages are cross platform.
Expert hardware knowledge are required to Expert arithmetic and logical skills are required to program in high
4
program in low level languages. level languages.
Examples: Machine Language and Assembly
5 Examples: C, C++, Java, C#, Python and JavaScript etc.
Language etc.
9
iii. What is IDE?
IDE (Integrated Development Environment)
IDE stands for Integrated Development Environment, it is a programming language
development tool used to create, compile, debug and run the computer program. IDE is a
computer software that include editor, compiler, linker, loader, debugger, library
functions and processes to develop a computer program in one environment. Modern
IDE have GUI based user friendly interface.
For example; Turbo C, C-Free, Visual studio are the IDE’s used for C Languages.
10
In C language if a program is successfully compiled (translated) then compiler
generates its object file with an extension of .obj that contains the machine or
intermediate code and an executable (.exe) file that is linked with the object file and
next time run the program directly from the executable file.
Languages like C, C++ and Java etc. uses compiler.
Interpreter
An Interpreter is also a system software and high level language translator which is
used to translate the high level programming language code into low level language
code statement by statement (line by line) and if there is any error occurs it stops the
translation and display the error in that line. It doesn’t generate any object and
executable file. Interpreter translates the code every time when program runs.
Languages like Basic, JavaScript and Python etc. uses interpreter.
11
viii. Differentiate between source program and object program?
Source Program Object Program
Source code is another name for source Object code is another name for object
program. program.
Programmer can read the source program. Machine can read the object program.
Programmer creates the source program. Compiler creates the object program.
12
xi. What is purpose of const qualifier?
Const qualifier – const (constant)
The const qualifier is used to define a constant. The constant must be initialized by
specifying its name, data type and some values. The initialized value cannot be changed
during program execution.
The General Syntax of declaring constant with const qualifier is as follow:
Const data type identifier = value;
Where
Const: is a keyword used to define a constant.
Data type: indicate the data type of the constant.
Identifier: represent the name of the constant.
Value: represent the value with which the constant is initialized.
Example:
const int N = 100;
const float R = 3.5;
The same can be achieved using #define preprocessor as:
#define N 100
#define R 3.5
13
program in low level language requires a deep knowledge of the internal structure of
computer hardware.
There are two types of low-level language.
i. Machine language
ii. Assembly Language
i. Machine language
A type of programming language in which instructions are written in binary form
is called machine language. Program written in machine language can be
executed rapidly by the computer. Machine language is represented inside the
computer by string of binary digits (bits) 0 and 1. The symbols 0 stand for the
absence of an electric and the 1 stands for the presence of an electric pulse.
Machine language is also known as first generation language.
ii. Assembly language
Assembly language is a low level language. It is one step higher than machine
language, symbols are used instead of binary code. These symbols are called
mnemonics. For example, sub instruction is used to subtract two numbers.
It is also called symbolic language. The instruction of the assembly language are
converted to machine code by a language translator called assembler and then
they are executed by the computer. Assembly language is also known as second
generation language.
Q4. What is high level language? Explain any five types of high level
languages?
A high-level language generally independent of the computer`s hardware architecture
and enables the development of a computer programs in more user-friendly
programming context.
i. C Language
C is a general-purpose computer programming language developed in 1972 by
Dennis Richie at AT&T Bell Laboratories USA. C is a simple and structure oriented
procedural programming language. It is highly portable among platforms and
14
therefore widely used in industry and among computer professionals. The operating
system UNIX was written in C.
ii. C++ Language
C++ is an extension of C language. It is an Object-Oriented Programming (OOP)
language. C++ includes all the elements of the C language and it has additional
features for working with objects, classes, events and other object-oriented
concepts. Most of the application program, such as word processers and
spreadsheets are developed in C++.
iii. Visual Programming Languages
The term visual programming refers to creating or developing Window-based
application or Graphical User Interface (GUI) applications. The visual programming
language are also called fifth-generation language based on object-oriented
programming. Some popular and widely used visual programming languages are
Visual Basic (VB) and Visual C++, C#, AND java.
iv. C# language
C# or C-sharp is an object-oriented programming language. It was developed in
2000 by Microsoft Corporation. It is based on C++ with elements from Visual Basic
and Java. It is used to write web applications that can be used across the Internet.
ALL types of programming including games, utilities, operating systems, compilers,
business applications and web based application can be developed in C#.
v. JAVA Language
Java is a high-level object oriented-programming language developed by Sun
Microsystems in 1991. It is very similar to C++ but it is especially used for web
page designing. APPLETS are Java program that are embedded into web pages to
rotate images, for animation and to access database on the internet.
15
i. Preprocessor directives
The instructions that are given to the compiler before the start of the actual
program are called Preprocessor directives. These are also known as Compiler
directives. These preprocessor directives normally start with hash sign # and the
keyword include or define, e.g., #include, #define, etc.
The #include directives allows external header file to be processed by the
compiler. For example #include<studio.h>
ii. The main( ) function
The main function must be present in every C program. it is the Point where C
program starts execution. If main function is not included in the program then
compiler gives error message.
The word main is followed in the by a pair of parenthesis ( ).
The Syntax of the main ( ) function is:
void main ( )
{
Program statements;
}
iii. Body of main function
After parenthesis of main function, the body of main function starts with an
opening curly “{” braces and ends with a closing curly brace “}”. When main
function is executed, the instructions written in it perform their task. . Inside the
body of main function, C programming statement are written to perform a specific
tasks.
16
i. Integer (int, long ) Type
Integer are whole number with a machine dependent range of values. C has 3 classes
of integer storage namely short, int and long variable. General Syntax for declaring
int variable
int variable_name ;
Generally, the number of bytes set aside by the compiler for the above Datatype are as
given in the table.
-32,768 to +32,767
int 2
-2,147,483,648 + 2,147,483,647
long 4
0 to 65,535
Unsigned int 2
17
Q7. What are constants and variables? Differentiate them with example?
Variables
Variable are the containers for storing data values. It
represents a storage or memory location in the
computer memory. The data stored in variables is
temporary which can be used or processed only during
the execution of the program. The data stored in
variables automatically removed when program ends.
In C language a variable can be numeric or character.
Constant
A constant is a quantity that cannot be changed during program execution. A
literal constant is a value that is typed directly in program. It appears in the program
wherever it is needed. For example, the following statement contains string constant:
“Pakistan Zinda Bad”
used as a label in printf function
printf(“Pakistan Zinda Bad”);
Similarly, in the following statement.
int age = 19;
Age is an integer variable and 19 is a literal constant.
18
Declaration of Variables
Assigning a known value to variable at the time of its declaration is called initialization of
the variable. General Syntax for initialization of a variable is
DataType variable_Name = value;
int x = 2; float
float y = 3, z;
z = x + y;
Unsiged long int
19
UNIT 3
20
iv. Differentiate between getch ( ) and getche ( ) functions?
getch ( ) Function:
The getch ( ) is used to read a single character from the keyboard without
displaying it on the screen.
General Syntax:
ch = getch ( );
getche ( ) Function:
The getche ( ) is used to read a single character from the keyboard with echo
i.e. echoing (displaying) it on the screen.
General Syntax:
ch = getche( );
b) 60/10*24+3
Solution:
= 6*24+8
= 144+8
= 152
c) 100%50-100%3
Solution:
= 0 - 100%3
=0-1
= -1
21
Compound Assignment Operator
C language provides a compound assignment operator that combine assignment
operator with arithmetic operators which are used to perform mathematical operations
more easily. The general syntax of compound operators are follows:
DataType Variable = expression;
For example:
a +=10 – is equivalent to a = a + 10;
a –=10 – is equivalent to a = a – 10;
a *=10 – is equivalent to a = a * 10;
Q3. What are operators? Explain different type of operators with examples?
Operators
Operators are the symbols that are used to perform certain operations on the data. C
language provides following types of operators;
a. Arithmetic Operators
b. Assignment Operators
c. Relational Operators
d. Logical Operators
a. Arithmetic Operators
Arithmetic operator is a symbol that performs mathematical operation on numeric
data. C language provides many arithmetic operators. The following are the list of
arithmetic operators used in C.
Operation Symbol Description
Addition + Add two values
Subtraction - Subtracts one value from another value
Multiplication * Multiplies two values.
Division / Divides one values by another value
Modulus % Gives remainder of division of two integers.
b. Assignment Operator
The assignment operator = is used to assign values to variables. It is used to assign
a numeric or character value to a variable. It has following General Syntax:
variable = Character_constant / variable / Expression;
For example;
int rollNo = 103;
char gender = ‘M’;
area = 3.14 * r * r ;
22
c. Relational Operator
The relational operator are used to specify conditions in a program. A relational
operators compares two values or a conditions and produces the result as true or
false. The relational operator are also called conditional or comparison operators.
C / C++ provides following relational operators:
Operator Description Example
> Greater than a>b
operator (return true if a is greater than b, otherwise
return false)
< Less than a<b
operator (return true if a is less than b, otherwise
return false)
== Equal to a == b
operator (return true if a and b are equal, otherwise
return false)
>= Greater than or a >= b
equal to (return true if a is greater than or equal to
operator b, otherwise return false)
<= Less than or a<=b
equal to (return true if a is less than or equal to b,
operator otherwise return false)
!= Not equal to a != b
operator (return true if a is not equal to b, otherwise
return false)
d. Logical Operator
The logical operators are used to evaluate compound conditions. There are three
logical operator in C Language.
23
Q4. What is output function? Describe in detail different type of output functions?
Output function
Functions that are used to receive the data from the computer memory and then display it
to the output devices is called output functions.
C – Language offers the following output functions
printf ( )
puts ( )
cout
a. printf ( ) function
printf ( ) is the most commonly used output function in C language. It is used to
display the data (i.e. messages, value of a variable or result of an expression) on the
output screen. It has the following general syntax
printf ([“escape sequence / format specifier”], “variable / Expression / String”);
(Note: escape sequence or format specifier is optional)
Example:
printf(“I Love Pakistan”);
printf(“%d”, a);
b. puts ( ) Function
puts ( ) function is used to display the string on the screen. It has the following general
syntax
puts (“string”);
Example:
puts (“Hello world!”);
c. cout
cout is generally used in C++ for obtaining the output on the screen. cout is basically
an object that can used by insertion operator <<. It has the following general syntax
cout<< Variable or constant or expression or string of character.
Example: cout<< “Hello this is my first program”;
Q5. Discus different format specifiers and escape sequences used in printf ( ) function?
Format Specifier used in printf ( )
The Format specifier in printf ( ) function is a string that is used according to the format
of output values. The format string always starts with a '%' character.
A list of commonly used format specifiers is given as follows
Format specifier Purpose
Signed integer
% d or % i
Long integer
% ld
Floating point (decimal notation)
%f
Floating point(exponential notation)
%e
Single character
%c
String
%s
24
Escape sequences used in printf ( )
An escape sequence are special non printed characters used to control the printing on
output devices. These are the combination of characters begin with back slash \ and
followed by a letter or special symbol. It can be used anywhere in the function to format
the output.
Escape
Purpose Example Output
Sequences
Used to move the Hello
\n printf(“Hello\nWorld”);
control to the New line World
\t Horizontal tab printf(“Hello\tWorld”); Hello World
Produce Bell or Beep Produce a beep
\a printf(“\a”);
sound sound
Backspace (used to
\b move cursor one space printf(“Foodo\bWorld”); FoodWorld
back after printing)
i. scanf ( )
The scanf ( ) function is used to read multiple data values of different data types from
the keyboard. The scanf () function is built-in function defined in a header file called
"stdio.h". It has following general syntax
scanf (“format specifier”,&var1,&var2,…...);
Example:
scanf (“%d”,&a);
ii. getch ( )
The getch ( ) function is used to read single character from the keyboard without
displaying it on screen (no echo). It has following general syntax
variable = getch ( );
Example:
char gender = getch ( );
25
iii. getche ( )
The getche ( ) function is used to read single character from the keyboard with
displaying it on screen (echo). It has following general syntax
variable = getche ( );
Example:
char gender = getche ( );
iv. getchar ( )
The getchar ( ) function requires pressing Enter key after entering the character. It has
following general syntax
variable = getchar ( );
Example:
char gender = getchar ( );
v. gets ( )
The gets ( ) function reads the entire string until user presses Enter key. It has
following general syntax
gets (array);
Example:
gets (name);
vi. cin
In C++ Programming language the input can be obtained at runtime from the
keyboard using cin. cin is an input stream and always used with extraction operator
>> and defined in a header file called "iostream.h". It pauses the program until user
enter the value from the keyboard and press Enter. The cin can accept both numeric
and character values It has following general syntax
cin>>variable1>>variable2>>variable3>>…..>>variableN;
Example:
cin>>a>>b; (where a and b is an integer type variable)
26
Q7. Write a program that reads three numbers and print their sum, product and
average.
Q8. Write a program that reads the base and height of a triangle and prints its area.
27
Q9. Write a program that reads temperature in Celsius, converts it into Fahrenheit and
prints it on screen.
Q10. Write a program that reads name and address of a student and prints it on the
screen using gets( ) and puts( ) functions.
printf("Name = ");
puts(name);
printf("Address = ");
puts(address);
}
28
UNIT 4
CONTROL STRUCTURE
Q2. 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.
#include<stdio.h> Output
#include<conio.h>
void main()
{
int num1,num2,num3;
printf("Enter First Number = ");
scanf("%d",&num1);
printf("Enter Second Number = ");
scanf("%d",&num2);
printf("Enter Third Number = ");
scanf("%d",&num3);
if(num1>num2)
{
printf("largest Number = %d \n", num1);
}
else
if(num2>num3)
{
printf("largest Number = %d \n", num2);
}
else
{
printf("largest Number = %d \n", num3);
}
}
29
[Link] a program in C to input a single character and print a message
“It is a vowel” or “It is a consonant”. Use if-else structure.
#include<stdio.h> Output
#include<conio.h>
void main()
{
char character;
printf("Enter Character = ");
scanf("%c",&character);
if(character == 'a' || 'A')
{
printf("It is a vowel\n");
}
else
if(character == 'e'|| 'E')
{
printf("It is a vowel\n");
}
else
if(character == 'i'|| 'I')
{
printf("It is a vowel\n");
}
else
if(character == 'o'|| 'O')
{
printf("It is a vowel\n");
}
else
if(character == 'u'|| 'U')
{
printf("It is a vowel\n");
}
}
30
[Link] the program in Q3 using switch-case structure.
#include<stdio.h> Output
#include<conio.h>
void main()
{
char character;
printf("Enter Character = ");
scanf("%c",&character);
switch(character)
{
case 'a':
printf("It is a vowel\n");
break;
case 'e':
printf("It is a vowel\n");
break;
case 'i':
printf("It is a vowel\n");
break;
case 'o':
printf("It is a vowel\n");
break;
case 'u':
printf("It is a vowel\n");
break;
}
}
31
[Link] out error if any in the following program.
#include<stdio.h> Error
#include<conio.h>
int main()
{ Condition is required after second if
int a; statement.
printf("Enter any number = ");
scanf("%f",&a);
if(a>10)
printf("value is greater than 10");
else
if
printf("value is less than 10");
}
32
UNIT 5
LOOP STRUCTURE
Q2. Explain the different type of looping structures with examples?
Looping Structure
A type of control structure that repeats a statement or set of statements until stated condition
becomes false is called a looping structure.
There are three types of loops available in C Language i.e.
1. for loop
2. While loop
3. Do-while loop
33
while (condition)
{
Body of the loop;
}
For Example
While ( i<=10)
{
printf(“%d”,i);
i++;
}
do
{
Body of the loop;
}
while(condition);
For Example
do
{
printf (“%d”,i);
i++;
} while ( i<=10);
Q3. Differentiate between do-while and while loops. Explain with Program
examples?
34
while (condition)
{
Body of the loop;
}
Example: A program to display “Pakistan” for the five times using the while loop.
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
n = 1;
while ( n <= 5)
{
printf (“Pakistan\n”);
n++;
}
getch( );
}
do
{
Body of the loop;
}
while(condition);
Example: A program to display “Pakistan” for the five times using the do – while
loop.
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
35
clrscr( );
n = 1;
do{
printf (“Pakistan\n”);
n++;
}
while ( n <= 5);
getch( );
}
Q4. Differentiate between do-while and while loops. Explain with Program
examples?
#include<stdio.h> Output
void main()
{
double i=2, product=1;
while(i<=100)
{
product = product * i;
i+=2;
}
printf("Product of even numbers from 1
to 100 = %ld\n", product);
}
#include<stdio.h> Output
void main()
{
int ch;
for(ch='A'; ch <= 'Z'; ++ch)
{
printf("%c\t",ch);
}
printf("\n");
}
36
Q6. Write a program to display the following output using nested for loop.
a a
a a a
a a a a
a a a a a
#include<stdio.h> Output
void main( )
{
int i,j;
for(i=1;i<=5;i++)
{
for(j= 1;j<=i;j++)
{
printf("a\t");
}
printf("\n");
}
}
#include<stdio.h> Output
void main()
{
int i = 1, sum=0;
for(i=1;i<=50;i++)
{
if(i%2 != 0)
{
sum = sum + i;
}
}
printf("Sum of odd numbers from 1 to 50
= %d\n",sum);
}
37
38
39
40
41
42