Algo Bann
Algo Bann
return sum
// Call the function to roll the dice and get the sum
dice_sum = roll_dice()
return lottery_numbers
Dana Sobh
Exercise 1
-Output-
Enter A: 5
Enter B: 6
The new value of A and B are A= 6, B=5
Dana Sobh
Exercise 2
-Solution-
Begin
variable: A,B,C : integers
print “Enter A, B and C”
read A,B,C
BA
CB
AC
print(“The new values of A=“ + A+ “,B=“ + B + “C=“ + C
End
Dana Sobh
Exercise 2
-Output-
Enter A, B and C
5, 8 ,9
The new value of A=9 B=5 C=8
Dana Sobh
Exercise 3
-Solution-
Variable t, r : integer
d,h,m,s :integer
Begin
print(“ Enter the time in seconds”)
read t
d t/(24*60*60)
r t%(24*60*60)
h r/(60*60)
r r%(60*60)
m r/60
s r%60
print(“The number of days=“ +d+ “h=“+h+ “m=“ +m+ “s=“ +s
End
Dana Sobh
Exercise 3
-Flowchart-
Dana Sobh
Exercise 4
-Solution-
variable A,B,C : real
output d:real
Begin
print(“Enter A”)
read A
print(“Enter B”)
read B
print(“Enter C”)
read C
d A*B*C
print(“The result is “ +d)
End
Dana Sobh
Exercise 4
-Flowchart-
Dana Sobh
Exercise 5
-Solution-
variable a,b,c,d,e : real
output f:real
Begin
print(“Enter 5 numbers”)
read a,b,c,d,e
f (a+b+c+d+e)/5
print(“The result is:” +f)
End
Dana Sobh
Exercise 5
-Flowchart-
Dana Sobh
Exercise 6
-Solution-
variable x,y,A: real
Begin
print(“Enter x”)
read x
print(“Enter y”)
read y
A sqrt(x*x + y*y)
print(“The result is:” +A)
End
Dana Sobh
Exercise 6
-Flowchart-
Dana Sobh
Write an algorithm that asks the user to enter his country and then display his language
Dana Sobh
Hint
Dana Sobh
Solution
• Solution of Question 11:
variable A: int
Begin
print(“Enter a number ”)
read A
fourthDigit A%10
thirdDigit (A/10)%10
secondDigit (A/100)%10
fistDigit (A/1000)%10
IF fistDigit + secondDigit == thirdDigit + fourthDigit then
Dana Sobh
Example
-While loop-
• Solution of Question
do
print “Enter a guess between 1 and 100”r
read guess
if (guess > num)
print(“Too high! Try Again ”)
else
print(“Too low! Try Again”)
tries ++
while (guess != num)
print(“Correct! You got it in” + tries + “guesses”)
End
Dana Sobh
Question
Dana Sobh
Solution
Begin
Var i, j, n: int
Print(“Enter number of rows”)
Read n
For(i=1;i<=n ;i++){
for(j=1;j<=i; j++){
print(j)
}
print(“\n”)
}
End
Dana Sobh
Question
Dana Sobh
Solution
Begin
Var i, j, n: int
Print(“Enter number of rows”)
Read n
For(i=n ;i>=1;i--){
for(j=i ; j>=1;j--){
print(j)
}
print(“\n”)
}
End
Dana Sobh
Question
• Using nested while loop
Dana Sobh
Solution
Begin
Var i=0 : int
While( i <10){
int j=0
while(j<=i){
print(*)
j++
}
i++
}
Dana Sobh
Question
Using nested for loop
Dana Sobh
Solution
Begin
Var i, j, n: int
Print(“Enter number of rows”)
Read n
For(i=1;i<=n ;i++){
for(j=1;j<= n - i; j++){
print(“ ”)
for(j= 1; j<= i ; j++){
print(“*”)
}
}
print(“\n”)
}
End
Dana Sobh
Question
Dana Sobh
Solution
2 *i -1
Begin
Var i, j, n: int
Print(“Enter number of rows”)
Read n
For(i=1;i<=n ;i++){
for(j=1;j<= n - i; j++){
print( )
for(j= 1; j<= 2*i -1 ; j++){
print(“*”)
}
}
print(“\n”)
}
End
Dana Sobh
•Write a function that takes a list of numbers and returns a list with two elements:
•
•The first element should be the sum of all even numbers in the list.
•The second element should be the sum of all odd numbers in the list.
•
•Example
•sum_odd_and_even([1, 2, 3, 4, 5, 6]) ➞ [12, 9] # 2 + 4 + 6 = 12 and 1 + 3 + 5 = 9
•
•sum_odd_and_even([-1, -2, -3, -4, -5, -6]) ➞ [-12, -9])
•
•sum_odd_and_even([0, 0]) ➞ [0, 0])
FUNCTION sum_odd_and_even(numbers)
// Initialize sums
sum_even = 0
sum_odd = 0