0% found this document useful (0 votes)
12 views66 pages

Class-8 Notes

The document provides a comprehensive overview of algorithms, including definitions, uses in programming, and steps for writing algorithms. It also covers flowcharts, their symbols, advantages, and control flow statements such as sequential, selective, and iterative workflows. Additionally, it includes examples of algorithms for various mathematical operations and programming constructs like if statements and loops.
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)
12 views66 pages

Class-8 Notes

The document provides a comprehensive overview of algorithms, including definitions, uses in programming, and steps for writing algorithms. It also covers flowcharts, their symbols, advantages, and control flow statements such as sequential, selective, and iterative workflows. Additionally, it includes examples of algorithms for various mathematical operations and programming constructs like if statements and loops.
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

SYLLABUS-VIII

Algorithm
1. What is an Algorithm?

An Algorithm is a set of sequential steps to solve


logical or a mathematical problem. Algorithm is
always written in simple language and precise
manner, so that every one can understand the
steps. It is also known as “Pseudocodes”
2. What are the uses of Algorithm in Programming?

In mathematics and computer science, an algorithm is a


step-by-step procedure for calculations. Algorithms are
used for calculation, data processing, and automated
reasoning. Algorithms resemble recipes. Recipes tell you
how to accomplish a task by performing a number of steps.
3. What is the goal of studying Algorithm?

❖ To develop framework for instructing computer to


perform tasks
❖ To introduce notion of algorithm as means of specifying
how to solve a problem
❖ To introduce and appreciate methods for defining and
solving very complex tasks in terms of simpler tasks.
4. What are steps for writing an Algorithm?

To write an Algorithm following steps must be followed:-


❖ An Algorithm must have a definite starting point.
❖ Each steps of Algorithm must be clearly understood.
❖ It should be written in a particular order.
❖ Each step must be assigned with a serial number.
❖ It must be written in simple English Language.
❖ It must have a definite end point.
5. Write an Algorithm to find Sum of two numbers?

To Find Sum of Two Numbers follow the steps given below:-


1. Start
2. Read X and Y from the user / Input value of X and Y
3. Take Sum=0
4. Compute Sum= X+Y
5. Print Sum/ Display Sum
6. Stop.
6. Write an Algorithm to find Sum and Average of three
numbers?

To Find Sum and Average of Three Numbers follow the steps


given below:-
1. Start
2. Read A, B and C from the user
3. Take Sum=0, Average=0
4. Compute Sum= A+B+C
5. Compute Average=Sum/3
6. Print Sum
7. Print Average
8. Stop.
7. Write an Algorithm to calculate the Area of a Circle.

To Find Area of a Circle follow the steps given below:-


1. Start
2. Read Radius r from the user
3. Read =3.141
4. Take Area=  r 2
5. Compute Area= 3.141 x r x r
6. Print Area
7. Stop.
8. Write an Algorithm to calculate the Perimeter of a Circle.

To Find Perimeter of a Circle follow the steps given below:-


1. Start
2. Read Radius r from the user
3. Read =3.141
4. Take Perimeter= 2 r
5. Compute Perimeter= 2x3.14 x r
6. Print Perimeter
7. Stop.
9. What do you mean by Jump statement in Algorithm and
how it is done in Algorithm?

Ans. Diverting the flow of execution to some other part of the


program is called as jump in algorithm. A jump in Algorithm is
done with the help of go to statement.
10. Write an Algorithm to compare 2 numbers and find the
greater one.

To Find greater of 2 numbers follow the steps given below:-


1. Start
2. Read Num1 and Num2
3. Check if Num1>Num2. if true, move to step 4, else go to
step5
4. Print Num1 is greater than Num2
5. Check if Num1<Num2. if true, move to step 6, else go to step
7.
6. Print Num2 is greater than Num1
7. Print Num1 is equal to Num2
8. Stop.
11. Write an Algorithm to print the table of five(5).

To print table of 5 numbers follow the steps given below:-


1. Start
2. Read Num=5 and Count=1
3. Multiply= Num x Count
4. Print Multiply
5. Increase the value of count by 1 i.e. Count=Count+1
6. If Count<=10 then go to step-3 else go to step-7
7. Stop
12. Write an Algorithm to check whether a person can vote
or not.

To check whether a person can vote or not, follow the steps


given below:-
1. Start
2. Read Age as number from user
3. If (Age>=18) go to Step-4 Else go to Step-5
4. Print “ You can Vote”
5. Print “ Sorry! You cannot Vote”
6. Stop
Flowchart
1. What is Flowchart?

A Flowchart is a pictorial or graphical representation of


an algorithm. It is a type of diagram that represents an
algorithm or process, showing the steps as boxes of
various kinds, and their order by connecting them with
arrows. A Flowchart is done with the help of some
special symbols and flowlines.
2. What are the symbols used in a Flowchart?
Ans.- The Symbols used in a Flowchart are:-
Flowlines- Flowline shows the direction in which the
program is moving. These arrows are used to connect
other flowchart symbols with one another. It indicate
the flow of the logic of the program.
Oval/ - It is used represent the starting
Terminal and ending of a flowchart.

Input/ Output
Box - Itis used to accept inputs and outputs
in a Flowchart. Parallelogram symbol.
Processing Box
- It is used to write the processing
operation and doing calculations.
(Rectangle Shape)

Decision - It is used for checking and applying


Box
any conditions in the program. It has one
entry and two exit paths which is used for
Yes or No option. (Diamond / Kite Shape)

-It is used to connect a Flowchart


Connectors
described in multiple pages.
3. Draw a Flowchart to find the sum of two numbers?
4. Draw a Flowchart to find the Simple Interest.
5. Draw a Flowchart to find the greater of two numbers.
6. Draw a Flowchart to find the greater of three numbers.
7. Draw a Flowchart to find a given number as Even or
Odd.
8. Draw a Flowchart to find area of a circle.
9. Draw a Flowchart to find Perimeter of a Rectangle.
10. What are the advantages of a flowchart?

Ans. The advantages of Flowchart are:-


• Better Communication:- As a flowchart is a pictorial
representation of a program therefore it is easier to explain
the logic of a program.
• Better Documentation- As Flowchart is easily understood,
therefore it serves as a better document for understanding
the program in future.
• Efficient Coding- An actual program code can be easily
written from a flowchart, because it serves as a roadmap,
thus error from the program can be easily seen and
rectified.
• Easier Debugging:- Flowchart helps in easily detecting the
mistakes/ error in the logic of the program. Thus program
can be easily debugged(error free) because error can be seen
clearly in the flowchart which can be easily traced and
removed easily.
• Systematic Testing- it is easier to test the correctness and
proper working of the program. Since it involves less number
of steps and statements.
11. What is Control Flow Statement and what are
its type?
Ans. The Control Flow Statement defines or shows the flow of a
program. It is basically of 3 types:
1. Sequential Flow Statement.
2. Selective Flow Statement.
3. Iterative Flow Statement.
12. Describe Sequential Work Flow Statement.

Ans. In Sequential Work Flow statement various activities are


executed one after the other. It is not possible to go backwards
as execution always moves forward. Some of the examples of
sequential work flow statement are- addition of two numbers,
multiplication of two numbers, average calculation, recipe
book cooking items method, etc. It is a step by step serial
process.
13. Describe Selective Work Flow Statement.

Ans. In Selective Work Flow statement user has to select one


option from various options. The selective work flow statement
is also known as conditional statements. It is mainly based on
test condition. If the statement inside the expression is true
then the statement works else it does not execute. In many
situations, we need to take decisions based on available
options, for example if it is raining, we will stay at home
otherwise we will go outside. The Selective Work Flow
statement is of 2 types:-
1. If Statement
2. Switch Statement
14. Describe If Statement of Selective Work Flow.

Ans. In Selective Work Flow statement, If condition is again of


3 type:-
1. Simple If Statement.
2. If Else Statement.
3. Nested if /Multiple if Statement.
15. Describe Simple If Statement of Selective
Work Flow.

Ans. The Simple If statement block will run if the condition is


true. The syntax is given below:-

if (condition)
{

//Block of if statements here


//These statements will only execute if the condition is true

}
16. Write an example of Simple If Statement of
Selective Work Flow.

Ans. The Example of Simple If statement is given below:-

if (x%2 = = 0)
{

Print “Even Number”

}
17. Describe If Else Statement of Selective Work
Flow.
Ans. The If Else statement returns both values. If the condition
is True than a successive statement is displayed and if the
condition is false, a statement for false condition will be
displayed for else part. The syntax is given below:-
if (condition)
{
//Block of if statements for True condition here
}
else
{
// Block of Else for False condition here
}
18. Write an example of If Else Statement of Selective Work
Flow.

Ans. The Example of Simple If statement is given below:-

if (x%2 = = 0)
{
Print “Even Number”
}
else
{
Print “Odd Number”
}
19. Describe Nested If Statement of Selective Work Flow.
Ans. The Nested If statement checks for multiple conditions or
more than one condition. The syntax is given below:-
if (condition 1)
{
//Block of statements 1 here
}
else if(condition 2)
{
// Block of statements 2 here
}
else if(condition 3)
{
// Block of statements 3 here
}
else
{
// Block of Else for False condition here
}
20. Describe Nested If Statement of Selective Work Flow.
if (X==1)
else if(X==5)
{
{
Print “ MONDAY “
Print “ FRIDAY “
}
}
else if(X==2)
else if(X==6)
{
{
Print “ TUESDAY “
Print “ SATURDAY “
}
}
else if(X==3)
else if(X==7)
{
{
Print “ WEDNESDAY “
Print “ SUNDAY “
}
}
else if(X==4)
else
{
{
Print “ THURSDAY “
“SORRY! ENTER WRONG NUMBER”
}
}
21. Describe Switch Statement of Selective Work Flow.
Ans. The switch statement is used to evaluate several statements
based on the value of the condition. It is checked for each case
against the value of the condition until a match is found. The
break statement is also used along with every case to skip the
execution further. If the condition is not matched then default
statement is used. The syntax is given below:-
switch(condition 1)
{
Case 1:
Statement1;
Break;
Case 2:
Statement2;
Break;
Case n:
Statement n
Break;
Default:
Statement;
}
22. Describe Switch Statement of Selective Work Flow.
switch(X)
{
case:5
case:1
Print “ FRIDAY “
Print “ MONDAY “
break;
break;
case:6
case:2
Print “ SATURDAY “
Print “ TUESDAY “
break;
break;
case:7
case:3
Print “ SUNDAY “
Print “ WEDNESDAY “
break;
break;
default:
case:4
Print “SORRY! ENTER WRONG NO.”
Print “ THURSDAY “
}
break;
23. Describe Iterative Work Flow Statement.

Ans. In Iterative Work Flow statement an activity can be


continuously repeated for a fixed number of times. Iteration is
also known as Looping. There are three essential conditions
necessary for a loop to execute, they are- (a) A Starting or
initial variable with a start value.(b)A Test condition or
expression up to which a loop can execute. (c) An increment or
decrement of the variable for a fixed number of times. The
Iterative Work Flow statement is of 2 types:-
1. Entry Control Loop/ Top Tested Loop.
2. Exit Control Loop/ Bottom Tested Loop.
24. Describe Entry Control Loop of Iterative
Work Flow Statement.
Ans. In Entry Control Loop, the Test Condition is checked at
the beginning of the loop. If the test condition is True, then the
loop body will executes and if the condition is False, then the
loop will not execute. It is also called as Top Tested Loop. It is
basically of two types:-
1. While loop.
2. For Loop.
25. Write a note on While loop of Entry Control Loop
of Iterative Work Flow Statement.
Ans. The While loop is a top tested loop, as the condition is
checked at the entry or top of the loop. Here a counter variable
is declared before the loop. The test condition or test expression
is defined along with the While statement. The increment or
decrement operation is done inside the body of the loop. The
While loop body will only execute if the test expression is true.
Syntax-
Counter variable with initial value
While(test condition)
{
Print(Counter variable or statement)
Increment / decrement counter variable
}
26. Write an example of While loop of Entry Control
Loop of Iterative Work Flow Statement(Increment).
Example of While loop in Forward Direction-
int i=1;
While(i<=10)
{
Print(“i is =“,i);
i=i+1; //i++
}
Output:-
i is=1
i is=2
i is=3
i is=4
i is=5
i is=6
i is=7
i is=8
i is =9
i is=10
27. Write an example of While loop of Entry Control
Loop of Iterative Work Flow Statement(Decrement).
Example of While Loop in Reverse Direction-
int i=10;
While(i>=1)
{
Print(“i is =“,i);
i=i-1; //i--
}
Output:-
i is=10
i is=9
i is=8
i is=7
i is=6
i is=5
i is=4
i is=3
i is =2
i is=1
28. Write a note on For loop of Entry Control Loop of
Iterative Work Flow Statement.
Ans. The For loop is also a top tested loop, as the condition is checked at the
entry or top of the loop. Here the counter variable, the test condition or test
expression and the increment or decrement operation are written in a single
line. In for loop we have exactly two semicolons, one after initialization and
second after condition. In this loop we can have more than one initialization
or increment/decrement, separated using comma operator. for loop can have
only one condition. The for loop body will only execute if the test expression is
true.
Syntax-
For (initialization; test condition; increment/decrement)
{
Print(statement)
}
29. Write an example of For loop of Entry Control Loop
of Iterative Work Flow Statement(Increment).
Example of For loop in Forward Direction-
Counter variable with initial value
For (int i=1; i<=10; i++)
{
Print(“i is =“,i);
}

Output:-
i is=1
i is=2
i is=3
i is=4
i is=5
i is=6
i is=7
i is=8
i is =9
i is=10
30. Write an example of For loop of Entry Control Loop
of Iterative Work Flow Statement(Decrement).
Example of For loop in Reverse Direction-
Counter variable with initial value
For (int i=10; i>=1; i- -)
{
Print(“i is =“,i);
}

Output:-
i is=10
i is=9
i is=8
i is=7
i is=6
i is=5
i is=4
i is=3
i is =2
i is=1
31. Describe Exit Control Loop of Iterative Work
Flow Statement.
Ans. In Exit Control Loop, the Test Condition is checked at the
bottom or exit point of the loop. Here the loop will execute for
once even if the test condition is false. Thereafter, If the test
condition is True, then the loop body will continue executing
until the condition is False. It is also called as Bottom Tested
Loop. It is basically of one type:-
1. Do While loop.
32. Write a note on Do While loop of Exit Control Loop
of Iterative Work Flow Statement.
Ans. The Do While loop is a bottom tested loop, as the condition is checked
at the bottom or exit of the loop. Here a counter variable is declared before
the loop. The increment or decrement operation is done inside the body of the
loop after the statement. This loop will execute the code block once, before
checking the condition. If it is true, then it will repeat the loop as long as the
condition is true. The loop will always be executed at least once, even if the
condition is false, because the code block is executed before the condition is
tested:
Syntax-
Counter variable with initial value
Do
{
Print(statement)
Increment / decrement counter variable
}
While(condition);
33. Write an example of Do While loop of Exit Control
Loop of Iterative Work Flow Statement(Increment).
Ans. Example of While loop in Forward Direction-
int i=1;
Do
{
Print(“i is = ” , i);
i=i+1; //i++
}
While(i<=10);
Output:-
i is=1
i is=2
i is=3
i is=4
i is=5
i is=6
i is=7
i is=8
i is =9
i is=10
34. Write an example of Do While loop of Exit Control
Loop of Iterative Work Flow Statement(Decrement).
Ans. Example of While loop in Reverse Direction-
int i=10;
Do
{
Print(“i is = ” , i);
i=i-1; //i--
}
While(i>=1);
Output:-
i is=10
i is=9
i is=8
i is=7
i is=6
i is=5
i is=4
i is=3
i is =2
i is=1
35. Describe about the Jump Statement used in Loop.

Ans. Sometimes, while executing a loop, it becomes necessary


to skip a part of the loop or to leave the loop as soon as certain
condition becomes true, that is jump out of loop. Programming
language allows jumping from one statement to another within
a loop as well as jumping out of the loop.
1) break statement:-When break statement is encountered
inside a loop, the loop is immediately exited and the program
continues with the statement immediately following the loop.
2) continue statement:-It causes the control to go directly to the
test-condition and then continue the loop process. On
encountering continue, cursor leave the current cycle of loop,
and starts with the next cycle.
36. Write an example of While loop having Jump
Statement(Break).
Example of While loop having Jump Statement- break
int i=1;
While(i<=10)
{
Print(“i is =“,i);
i=i+1; //i++
if (i == 5)
{
Break;
}
}
37. Write an example of While loop having Jump
Statement(continue).
Example of While loop having Jump Statement- continue
int i=1;
While(i<=10)
{
If (i == 4)
{
i=i+1; //i++
continue;
}
Print(“i is =“,i);
i=i+1; //i++
}
38. Write an example of For loop having Jump
Statement(break).
Example of For loop having Jump Statement- break
Counter variable with initial value
For (int i=1; i<=10; i++)
{
if (i == 5)
{
break;
}
Print(“i is =“,i);
}
39. Write an example of For loop having Jump
Statement(continue).
Example of For loop having Jump Statement- continue
For (int i=1; i<=10; i++)
{
if (i == 5)
{
continue;
}
Print(“i is =“, i);
}
40. Write about the Python Logical Conditions for
Maths.
Ans. Python supports the usual logical conditions from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if


statements" and loops.
An "if statement" is written by using the if keyword.
41. Write about the Python Elif Keyword and Example
of it.
Ans. The elif keyword in python is the way of saying "if the
previous conditions were not true, then try this condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

In this example a is equal to b, so the first condition is not true,


but the elif condition is true, so we print to screen that "a and b
are equal".
42. Write about the Python Else Keyword and Example
of it.
Ans. The else keyword catches anything which isn't caught by the
preceding conditions.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

In this example a is greater than b, so the first condition is not


true, also the elif condition is not true, so we go to
the else condition and print to screen that "a is greater than b".
43. Write an example of Python Else Keyword without
Elif.
Ans. The example of else keyword without Elif is as follows:-
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

In this example a is greater than b, so the first condition is not


true, so we go to the else condition and print to screen that “b is
not greater than a".
44. Write an example of Python Shorthand If.

Ans. If
you have only one statement to execute, you can put it
on the same line as the if statement.The example of
Shorthand If is as follows:-
a = 200
b = 33
if a > b: print(“a is greater than b")

In this example a is greater than b, so it will print to screen that “a


is greater than b".
45. Write an example of Python Shorthand If…Else.

Ans. If
you have only one statement to execute, one for if, and
one for else, you can put it all on the same line:The example
of Shorthand If…Else is as follows:-
a=2
b = 330
print("A") if a > b else print("B")

In this example b is greater than a, so the first condition is not


true, so we go to the else condition and print to screen will be “B”
46. Write an example of Python Shorthand Multiple
Else Statement
Ans. User can also have multiple else statements on the same
line. This technique is known as Ternary Operator
or Conditional Expression.The example of Shorthand
Multiple Else is as follows:-
a = 330
b = 330
print("A") if a > b else print("=", "Equal To") if a == b else print("B")

In this example both a and b are equal, so the middle condition is


will be printed to screen and output will be “= Equal To”
47. Write an example of Python While loop having
Jump Statement(Break).
Ans. The while loop requires relevant variables to be ready. In
this example we need to define an indexing variable, i,
which we set to 1. With the break statement we can stop the
loop even if the while condition is true:
i=1
while i < 10:
print(i)
if i == 5:
break
i += 1 #i=i+1

Note: remember to increment i, or else the loop will


continue forever.
48. Write an example of Python While loop having
Jump Statement(Continue).
Ans. The while loop requires relevant variables to be ready. In
this example we need to define an indexing variable, i,
which we set to 0. With the continue statement we can stop
the current iteration, and continue with the next:
i=0
while i < 10 :
i += 1 # i=i+1
if i == 5:
continue
print(i)

Note: Remember that number 5 will be missing in the


result
49. Write an example of Python For loop having Jump
Statement(break).
Ans. A for loop is used for iterating over a sequence (that is
either a list, a tuple, a dictionary, a set, or a string). With
the break statement we can stop the loop before it has
looped through all the items:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break

Note: Exit the loop when x is "banana":

You might also like