C++ 2
C++ 2
Name ID
1, Dagim Biruk ………………………………….. 1700546
2, Dagmawi Dawit……………………………… 1700548
3, Ayele Seyife ……………………………………. 1700391
4, Bemirew
5,
6,
7,
8,
9,
10,
1. Write an algorithm description and draw a flow chart to calculate the
average of 4 integer numbers.
Algorithm
Step 1: start
Step 2: read a, b, c, d
Step 3: sum= a + b +c + d
Step 4: A = sum/4
Step 5: print A
Step 6: stop Flow chart
start
Input a, b,
c, d
Sum=a + b + c + d
A=sum/4
Print A
Stop
2, write an algorithm description and a flow chart to find the
largest value of any three numbers.
Algorithm
Step 1: start
Step 2: read a,b,c
Step 3: compare a and b
If a>b, then compare a with c:
If a>c, then a is the largest
Else c is the largest
Else (when b>a ), compare b with c:
If b>c, then b is the largest
Else , c is the largest
Step 4: return the identefid the largest value
Step 5: stop
Flow chart
start
read : a ,b, c
Yes No
Is a>b ?
Yes No No Is b>c
Is a>c ?
? ????? Yes
stop
3, Write an algorithm description and draw a flow chart which gats
a natural numbers N and display odd number equal or less than N.
Algorithm
Step 1: Start
Step 2: Input the value of N
Step 3: initialize i=1
Step 4: if (i<=N) then
Go to the next step
o Else: go to step 8
Step 5: if (i % 2=1) then
Print “i”
o Else go to the next step
Step 6: compute i=i+1
Step 7: go to step 4
step 8: stop
Flow chart
start
Input N
Initialize i=1
NO
stop If i<=N
Yes
No
i=i+1
4, write an algorithm description and draw a flow chart to swap the
content of two variable using third variables .
Algorithm
Step 1: Start
Step 2: Input Two Numbers Say x, y and third variable t
Step 3: Display Before Swap Values x, y
Step 4: Assign t = x
Step 5: Assign x = y
Step 6: Assign y = t
Step 7: Display After Swap Values x, y
Step 8: Stop
Flow chart
start
Input x, y
Display x, y before
swap and t
t=x
X=y , y=t
Print x, y
after swap
stop
5, write an algorithm description and draw a flow chart to swap the
content of two variable without using third variable.
Algorithm
Step 1: start
Step 2: input two numbers say x, y
Step 3: display before swap values x, y
Step 4: Assign x= x + y
Step 5: Assign y= x-y
Step 6: Assign x= x-y
Step 7: display after swap value x,y
Step 8: stop
Flowchart
start
Input x, y
before
swap
X=x + y
y=x-y
x=x-y
Print x, y after
swap
stop
6, write an algorithm description and draw a flow chart to the
factorial of any number.
Algorithm
Step 1: start
Step 2: read number
Step 3: set fact=1, i=1
Step 4: check condition i<=number
if false go to step 7
Step 5: fact=fact*i
Step 6: update i=i+1 go to step 4
Step 7: display fact.
Step 8: stop
Flow chart
. start
Read
number
Fact=1
I=1
Fact=fact*i
stop
i=i+1
7, write an algorithm description and draw a flow chart to add
even number from 1 up to 100.
Algorithm
Step 1: start
Step 2: read i=1
Step 3: initialize sum=0
Step 4: if (i<=100) then
go to next step
else display sum then
go to step 9
Step 5: if (i%2==0) then
go to next step
else go to step 7
step 6: compute=sum=sum + i
step 7: compute i=i+1
step 8: go to step 4
step 9: stop
Flow chart Start
Read i=1
Initialize sum=0
Yes
Display sum No If i<=100
YES
YES NO
Stop if i%2=o
Sum=sum + i I=i+1
Flowchart
start
Yes No
Print real
root
stop