0% found this document useful (0 votes)
93 views24 pages

Computer Science P2

An algorithm for baking a cake: 1. Get the ingredients - eggs, sugar, milk. 2. Mix the eggs, sugar, and milk together for 10 minutes. 3. Pour the batter into a cake pan and microwave for 10 minutes. 4. If the cake is still raw, microwave for 5 more minutes.

Uploaded by

hdfajkh
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)
93 views24 pages

Computer Science P2

An algorithm for baking a cake: 1. Get the ingredients - eggs, sugar, milk. 2. Mix the eggs, sugar, and milk together for 10 minutes. 3. Pour the batter into a cake pan and microwave for 10 minutes. 4. If the cake is still raw, microwave for 5 more minutes.

Uploaded by

hdfajkh
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/ 24

Algorithm

Q : write an
algorithm for baking a cake .

*
get all the
ingredients -

take bowl and crack 2 in it


a
eggs
• .

• mix 3
tablespoons of sugar into it .

• add 500mL of milk .

• beat 10 min .

in

pour a cake pan



Microwave for 10 min .

if cake is still .

raw


then microwave for 5 more min .
Q write
: an
Algorithm for adding
3 numbers
together .

Ans : • Ask the user to enter first number


and store it .

• " " " "


a second "

7 >
4 5

• " " " " y third ✗

" " " -

• Add all these stoned values .

• Show the sum to user .

Pseudocode

{
start
"
Hello World
"
Print

End

start
"
.
Print
"
Enter first number :

Input numb
"
.

Print " Enter second number :


Input num2

.
Sum 8- numb + mum 2

Print Your is !
"
. sum , sum .

End

|
/ 3 5 8

mum 2 num2 Sum


Math operator

I
3
+ +
£ < =

- -

✗ * ⇐
÷ 1

ab
^
AND
OR
( ) ( )
NOT

Assignment
start

declare num1,num2 , sum as Real

numb ← 10

|n¥
2 33
mum ←
iiiz 's!!
sum ← num1+num2 + too

print sum

End
Conditional statement


if É
then

end
if

Q: Tell
if pass or fail input , marks from user
.

Start
"
Please
"
Print enter marks
your
:

marks
Input
if (marks >= 75 )
then
"
Print passed with good marks!
"
You
else if ( marks > 50) =

then "
Print " fair
you passed
with mairks!
else
You Failed !
"
"
Print
end if

End nestedifstaenent
Case statement

marks
case
of
"
90 : output ''
A *

380 : output "


B
"

"
output
"
270 : C

"
760 : output
"
D

"
3 50 : output KE

"
< 50 : Output of

Otherwise
"
Output
"
Enter valid marks
End case
LOOPS
• FOR
-
loop ← runs
for a
specific
amount time
of .

• While loop c- based on condition

Repeat -
Until loop ←
based on condition

For c ← 1 TO 10
EI
f. output c
c- 2
End for

123456789102
while marks↳#- ←

pre
Éntw marks
condition

End while
I

Repeat
: ,
¥1 MarkI Post
condition
Datatypes

Integers :O , 1
,
10 , 986 ,
-192

Real : 0.1 ,
9.36 ,
- I -2 1.0
,

' ' ' ' ' '

Char : A ! O
g g

String
" "
" " "

!* # Hello
"

Haifa 2
:
, ,

Boolean : 0,1 { False ,


True
Q write that Inputs
a
program
:

two numbers and performs ari thematic

operation on them as
specified by
tee user .

Start
declare numb
,
num 2 ,
ans as real
declare operator as char

" "

Output Please enter first number :

numb
Input

Please enter second


"
"
Output number :

Input mum 2

"
Output
"
Please enter operator :

Input operator

If
' '

operator = +

Then ans = numdt mum 2

' '

Elséff Operator
-

Then ans = mum 1 -


mum 2

'
ElseIf
'

operator = *

then ans = mum I * mum 2


Else If operator =
'
1)
2
Then ans = numb
/ mum

Else "

output please
"
enter valid operator

" "

Output Your answer is :


g
ans

End
case
of operator
' '
t ; ans = numb 1- mum 2

C- '
: ans = numb - num2

' '
* : ans = numd * Mumm

/
'
'
/ : ans = numb mum 2

otherwise output "


Plex enter valid operator :

end case
Flow charts

start / End

Input / Output

process

condition

.
Q Take two numbers from the user
and add them , show the result .

Start

Real mum
1) num2,5hm

Inputnumb,num2

Sum ← numb thank

1
Output sum

1
End
Q : Tell
if a standard is pass or
fail
his / her marks
depending on .

Start

int M

V
.

input M
.

15
" "

output you failed


"
>
M > 50
?

§
yes

/Outputiiyoupassed
<

Eid
start
for e= I to 10
" "
Output *

End for
End

start

<
repeat "TEtimes
^ ¥-7.0
Y

Outpolling
,

End
}
d
Is
NO
Age 218 > Output"Adult
"

omñ→]
Yes


Input PercentageMarks

÷
P.M < 0

OR

FM 7 1 00
xes→y
"/
Output Invalid Masks
"

?
ND

IS

f- p.ms

?
49

" "
Output Fail

µ es

⇐I£
-
"
Pass
"

End
Q :
keep
until
taking inputs from
valid
the user
he enters marks ?

start

✗input marks /
r-e☒i
t
Is

marks < 0 Yes


OR
marks > 100

marks =
80

Input marks

while Charles < 0 OR masks >


1001
Input marks

End While For


hoop ( specific lines)


While - end while

Repeat until
Repeat mark -_
48

Input marks

Until
(maxks > 0 AND marks < 100
)

Fori=3-o5-
2=5
output i
3,#
End
for

☐"
Pseudocode Syntax

Variable declaration

declare < variable name>


- as < datatype>
eg
:
declare numb as Real

Assignment statement

< variable - name > t < expression/variable>

a ← btc
eg :

Displaying message
"

output
"

any message

Output < variable


.name/ expression >

"
Output
"

message <variable name>


any ,
-
eg :

"

Output
"
Hello world !

Output a

Output numb + mum 2

Hello world !
"

Output
"

,
a

Taking Input
< variable name >
-

input -

eg :
Input numb
Selection statements

IF THEN
. .
- -
ELSE

IF < condition > THEN

END IF
} these statements
IF Block are

condition is true
in

executed
if
otherwise
they are skipped

IF < condition > THEN

IF marks 50 THEN
eg : =

passed
"
Output
"
You

ELSE
ELSE
" "

Output You Failed

END IF END IF
IF < condition > THEN

ELSE IF < condition > THEN

ELSE

END IF

END IF

You might also like