You are on page 1of 123

IT 11

Chapter 4
Algorithms and
Flowcharts
slides 1 – 57 only Mid-Term coverage
SECOND SEMESTER
SY 2023-2024
IT 11
Chapter 4
Algorithms and
Flowcharts
slides 1 – 57 only Mid-Term coverage
Flowchart and Steps Pseudocode
START Step1: Enter num1
Step2: Input num1
Step3: Enter num2
“Enter num1: ” Step4: Input num2
Step5:
Sum = num1 + num2; Diff = num1 – num2;
“Input num1: ” Product = num1 * num2; Quotient = num1 / num2;
Exponent = num1 ** num2; Int_div = num1 // num2;
Remainder = num1 % num2
“Enter num2: ” Step6:
Output Sum; Diff; Product; Quotient; Exponent; Int_div;
Remainder
“Input num2: ”
Program Code
Sum = num1 + num2; Diff = num1 – num2;
Product = num1 * num2; Quotient = num1 / num2;
Exponent = num1 ** num2; Int_div = num1 // num2;
Remainder = num1 % num2

Output Sum; Diff; Product; Quotient;


Exponent; Int_div; Remainder

STOP
9. There are two arrows for a selection symbol.
YES

NO
10. Define selection - is part of the code that involves
comparison may be used for counting, range and
value set. Normally you will see the comparison
with condition statement IS and IF inside.
statements:
IF-THEN, IF-ELSE IF – ELSE, FOR – NEXT,
CASE –SELECT and DO-WHILE.

11. 956 >= 856 TRUE


12. 123 != 123 FALSE
13. 55 = 66 FALSE
1.✔
Step1: START
Step2: Output mark out of 100
Step3: Input Mark
Step4: Is mark > 75
Step5: YES, Output “Distinction”
Step6: NO, Is mark > 60
Step7: YES, Output “Merit”
Step8: NO, Is mark > 45
Step9: YES, Output “Pass”
Step10: NO, Output “Fail”
Step11: STOP
1.✔
Step1: START
Step2: Output “Enter mark out of 100: “
Step3: Input Mark
Step4: Is mark > 75
Step5: YES, Output “Distinction”
Step6: NO, Is mark > 60
Step7: YES, Output “Merit”
Step8: NO, Is mark > 45
Step9: YES, Output “Pass”
Step10: NO, Output “Fail”
Step11: STOP
3. ✔
2.✔ Step1: START
OUTPUT “Enter mark out of 100: “ Step2: OUTPUT “Enter three numbers: “
INPUT mark Step3: Input num1
IF mark > 75
Step4: Input num2
OUTPUT “Distinction”
Step5: Input num3
ELSE IF mark > 60
OUTPUT “Merit” Step6: IF num1 < num2 and num1 < num3
ELSE IF mark > 45 Step7: YES, OUTPUT num1
OUTPUT “Pass” Step8: NO, IF num2 < num1 and num2 < num3
ELSE Step9: YES, OUTPUT num2
OUTPUT “Fail” Step10: NO, OUTPUT num3
END IF Step11: STOP
START

OUTPUT “Enter three


numbers: “

INPUT num1, num2,


num3

YES OUTPUT
3. ✔ ALGORITHM Steps and Flowchart IF num1< num2
&& num1 < num3 num1
Step1: START
Step2: OUTPUT “Enter three numbers: “
Step3: Input num1 NO
Step4: Input num2
YES
Step5: Input num3 IF num2 < num1 OUTPUT
Step6: IF num1 < num2 and num1 < num3 && num2 < num3 num2
Step7: YES, OUTPUT num1
Step8: NO, IF num2 < num1 and num2 < num3
NO
Step9: YES, OUTPUT num2
Step10: NO, OUTPUT num3
Step11: STOP
OUTPUT num3 STOP
START

OUTPUT “Enter three


numbers: “

INPUT num1, num2,


num3

4. ✔ Pseudocode IF num1< num2


YES OUTPUT
&& num1 < num3 num1
OUTPUT “Enter three numbers: “
INPUT num1
NO
INPUT num2
INPUT num3 YES
IF num1 < num2 and num1 < num3 IF num2 < num1 OUTPUT
OUTPUT num1 && num2 < num3 num2
ELSE IF num2 < num1 and num2 < num3
OUTPUT num2 NO
ELSE
OUTPUT num3 OUTPUT num3 STOP
END IF
6. ✔ CASE - SELECT

OUTPUT “Enter three numbers: “


INPUT num1
INPUT num2
SELECT HIGHEST
CASE num1 < num2:
answer = num2 – num1
OUTPUT answer
CASE num2 < num1:
5. ✔ Pseudocode answer = num1 - num2
OUPUT answer
OUTPUT “Enter two numbers: “ CASE DEFAULT:
INPUT num1 OUTPUT “The two numbers num1 and num2 are equal”
INPUT num2
IF num1 < num2
answer = num2 - num1
OUTPUT answer
ELSE
answer = num1 – num2
OUTPUT answer
END IF
6. ✔ CASE – SELECT (OTHER SOLUTION)

num2 = int(input("Enter num2: "))


diff = num1 - num2
SELECT diff
CASE diff > 0:
print(diff)
CASE diff < 0:
print(-1 * diff)
CASE DEFAULT:
print("0")
6. ✔ CASE - SELECT

OUTPUT “Enter three numbers: “


INPUT num1
INPUT num2
SELECT (num1 > num2)
CASE TRUE:
answer = num1 – num2
OUTPUT answer
CASE FALSE:
answer = num2 – num1
OUPUT answer
CASE DEFAULT:
OUTPUT “The two numbers num1 and num2 are equal”
6. ✔ CASE – SELECT (SOLUTION 2)

#include <iostream>
using namespace std;

int main()
{
int num1;
int num2;
int answer;

cout << "Enter num1: ";


cin>> num1;
cout << "Enter num2: ";
cin>> num2;

switch(num1 < num2){


case true:
answer = num2 - num1;
cout<< answer;
break;
case false:
answer = num1 - num2;
cout<< answer;
break;

}
return 0;
}
#SAVE @ FILENAME: COUNT_CONTROLLED1
num = 0
for num in range (0, 6):
print(num)
#SAVE @ FILENAME: COUNT_CONTROLLED_AVERAGE
grade = 0
total = 0
counter = 0
for i in range(1, 11):
grade = int(input("Enter grade: "))
total = total + grade
counter = counter + 1
print(counter)
average = total / 10
print(average)
#SAVE @FILENAME: CONDITION_CONTROLLED1
count = 0
age = 0
while True:
age = int(input("Enter a age: "))
if age > 18:
count=count + 1
print("Correct age!")
print("number of attempts:" , count)
exit()
else:
count = count + 1
print("try again")
print("number of attempts:" , count)
save@filename: condition_controlled2
value = 1
while value < 100:
value = value * 2
print(value)
SAVE @ FILENAME: CONDITION_CONTROLLED3
total = 0
number = 0
while total < 100:
number = int(input("Enter a number: "))
total = total + number
print(total)
PRACTICAL ACTIVITY 4.04
save @ filename: condition_controlled4
total = 100
number = 0
counter = 0
while total > 0:
number = int(input("Enter a number: "))
total = total - number
counter = counter + 1
print(total)
print("It took this number of loops:" , str(counter) + " " + " to end")
PRACTICAL ACTIVITY 4.06 p. 69
SAVE @ FILENAME: CONDITION_CONTROL_Answer3
number = 0
count = 0
while True: INPUT:
number = int(input("Enter a number: ")) 50
if number < 100 or number > 200: Try Again!
count=count + 1 202
print(“Try again!") Try Again!
else:
125
print("Finally within the range!") Finally within the range!
exit()
START

number = 0 number=0
count = 0 count=0
while True:
number = int(input("Enter a number: ")) input number
if number < 100 or number > 200:
count=count + 1 YES
is number < 100
print(“Try again!") or number > 200 count = count + 1
else: NO
print("Finally within the range!") output “try
count = count + 1
exit() again!”

output “Finally
within the range!”

STOP
SAVE @ FILENAME: Condition_Control_Ans4

#Let the user enter the number to guess and allow the user to enter a number
#until it is equal to the correct number and display the number of attempts
counter = 0
num_entered = int(input("Enter a number: ")) Enter a number: 90
while True:
num = int(input("Guess the number entered:")) Guess the number entered: 88
if num == num_entered: Number is incorrect try again
print("Number is correct!")
Guess the number entered: 100
print("Total number of attempts is:", counter) Number is incorrect try again
exit()
else: Guess the number entered: 90
print("Number is incorrect try again") Number is correct
counter = counter + 1
UNTIL HERE ONLY FOR THE COVERAGE OF THE MID-TERM…….
Figure 4.38

SAVE @ FILENAME: DATE_YEAR


date = input("Enter the date (dd/mm/yyyy): ")
substring = date[6:10]
print("The year in the date entered above is:", substring)

OUTPUT:
Word Example 4.12

SAVE @ FILENAME: Alternate_Letter


counter = 0
position_letter = 1
string = input("Enter a string:") OUTPUT:
stringlength = len(string)
for letter in range(0, stringlength):
print(string[counter:position_letter])
counter = counter + 2
position_letter = position_letter + 2

Step1: OUTPUT “Enter a name:”


Step2: INPUT name
Step3: PROCESS stringlength= len(name)
Step4: PROCESS first = name[0:1].upper()
Step5: PROCESS restofName = name[1:stringlength-1].lower()
Step6: PROCESS word = first + restofName
Step7: OUTPUT word

SAVE @ FILENAME: Upper_FirstLetter


string = input("Enter a string: ")
stringlength = len(string)
s1 = string[0:1].upper()
s2 = string[1:stringlength].lower()
print(s1 + s2)

Practical Activity 4.07 Answer# 2
SAVE @ FILENAME: string_length_defined
word_length = int(input("Enter the string length: "))
word = input("Enter a string:")
substring = word[0:word_length] OUTPUT:
print(substring)

SAVE @ FILENAME: num_letter_concatenate


words = ["", "", "", ""]
fullword=""
number_letter = int(input("Enter number of letter: "))
for i in range(0, number_letter):
letter_enter = input("Enter a letter: ")
words[i] = letter_enter
fullword = fullword + letter_enter #concatenate
print(fullword)

Practical Activity 4.07 Answer# 3
#format of date: dd/mm/yyyy elif month == "05":
day = input("Enter day of birth: ") month = "May" OUTPUT:
month = input("Enter month of birth: ") elif month == "06":
year = input("Enter year of birth: ") month = "June"
s1 = day[0:2] elif month == "07":
s2 = month[0:2] month = "July"
s3 = year[0:4] elif month == "08":
month = "August"
if month == "01": elif month == "09":
month = "January" month = "September"
elif month == "02": elif month == "10":
month = "February" month = "October"
elif month == "03": elif month == "11":
month = "March" month = "November"
elif month == "04": elif month == "12":
month = "April" month = "December"
print(s1 + " " + month + " " +s3)
Challenge Activities:

1. Create an algorithm to ask the user to enter a string and make sure the string
will display the first letter in capital letter and the rest in small letters.✔

example:
december 🡪 December
DECEMBER 🡪 December

2. Create an algorithm that will ask the user to enter Firstname, Middlename
and
Family name and display the initials of the username in all capital letters.

3. Create an algorithm that will display a string in reverse.


Challenge Activities:

1. Create an algorithm to ask the user to enter a string and make sure the
string will display the first letter in capital letter and the rest in small
letters.

SAVE @ FILENAME: Upper_FirstLetter


string = input("Enter a string: ")
stringlength = len(string)
s1 = string[0:1].upper()
s2 = string[1:stringlength].lower()
print(s1 + s2)

OUTPUT:
Challenge Activities:

1. Create an algorithm to ask the user to enter a string and make sure the string
will display the first letter in capital letter and the rest in small letters.

example:
december 🡪 December
DECEMBER 🡪 December

2. Create an algorithm that will ask the user to enter Firstname, Middlename
and
Family name and display the initials of the username in all capital letters.✔

3. Create an algorithm that will display a string in reverse.


Challenge Activities:

2. Create an algorithm that will ask the user to enter Firstname, Middlename
and Family name and display the initials of the username in all capital letters.

SAVE @ FILENAME: FULLNAME_INITIALS


Firstname = input("Enter your firstname: ")
Middlename = input("Enter your Middlename: ")
Lastname = input("Enter your Lastname: ")
substring1 = Firstname[0:1]
substring2 = Middlename[0:1]
substring3 = Lastname[0:1]
print(substring1 + substring2 + substring3)

OUTPUT:
Code1:

InputString = input("Enter a string: ")


reverse_string = InputString[::-1]
print(reverse_string)

Code2:
#Python Program To Reverse A String.
def reverse(str1):
str2 = ""
for i in str1:
#define a empty string str2 and insert the character from
starting.
str2 = i + str2
return str2

str1 = input("Enter a string: ")


#print the reversed string.
print (reverse(str1))
SEATWORK# 1

1. 5.
String = “SUBROUTINE” str1 = lower(string)
str1 = length(string) output str1

2. 6.
str1 = substring(str1, 0, 3) str1 = String[::-1]
output str1 output str1

3.
str1 = char( str1, 1)
output str1

4.
string1 = “Information”
string2 = “Technology”
str1 = upper(substring(string1, 0, 4))
str2 = lower(substring(string2, 0, 4))
output str1 + str2
SUBROUTINES
array = [0, 0, 0]
for word in range(0, 1):
array[word] = input("Enter a word with only three letters: ")
display = (array[int(input("Enter the letter in array element you want to display:"))])
pos_letter1 = int(input("Enter the position of the first letter: "))
str1 = (array[word])[0:1]
for letter1 in range(pos_letter1):
print(str1)
pos_letter1 = pos_letter1 -1
display = (array[int(input("Enter the letter in array element you want to display:"))])
pos_letter2 = int(input("Enter the position of the first letter: "))
str2 = (array[word])[1:2]
for letter2 in range(pos_letter2):
print(str2)
pos_letter2 = pos_letter2 -1
display = (array[int(input("Enter the letter in array element you want to display:"))])
pos_letter3 = int(input("Enter the position of the first letter: "))
str3 = (array[word])[2:3]
for letter3 in range(pos_letter3):
print(str3)
pos_letter3 = pos_letter3 -1
print(str1 + str2 + str3)
Nested statement
are condition within a condition, it could be an IF statement inside a loop or a loop
within an IF – ELSE statement, IF-THEN-ELSE statement within another IF-THEN
–ELSE and it could be a LOOP in another LOOP (FOR-NEXT in another FOR-
NEXT)
SAVE @ FILENAME: Answer4.09_1
total = 0
option = input("Enter Yes or No to calculate:")
if option == "Yes":
number_add = int(input("Enter numbers to add: "))
for i in range(0, number_add):
num = int(input("Enter a number: "))
total = total + num
print(total)
else:
exit()
SAVE @ FILENAME: ANSWER4.09_2
start = 0
second = 1
#letter=""
word = input("Enter a word:")
stringlength = len(word)
for i in range(0, stringlength):
letter = word[start:second]
position_letter = int(input("position of the letter in the alphabet:"))
for p in range(0, position_letter):
print(letter)
start = start + 1
second= second + 1
start = 0 SAVE @ FILENAME: Answer4.09_2
second = 1
#letter=""
fullword = ""
word = input("Enter a word:")
stringlength = len(word)
for i in range(0, stringlength):
letter = word[start:second]
position_letter = int(input("position of the letter in the alphabet:"))
for p in range(0, position_letter):
print(letter)
fullword = fullword + letter
start = start + 1
second= second + 1
print("OUTPUT:",fullword)
array = [0, 0, 0]
for word in range(0, 1):
array[word] = input("Enter a word with only three letters: ")
display = (array[int(input("Enter the letter in array element you want to display:"))])
pos_letter1 = int(input("Enter the position of the first letter: "))
str1 = (array[word])[0:1]
for letter1 in range(pos_letter1):
print(str1)
pos_letter1 = pos_letter1 -1
display = (array[int(input("Enter the letter in array element you want to display:"))])
pos_letter2 = int(input("Enter the position of the first letter: "))
str2 = (array[word])[1:2]
for letter2 in range(pos_letter2):
print(str2)
pos_letter2 = pos_letter2 -1
display = (array[int(input("Enter the letter in array element you want to display:"))])
pos_letter3 = int(input("Enter the position of the first letter: "))
str3 = (array[word])[2:3]
for letter3 in range(pos_letter3):
print(str3)
pos_letter3 = pos_letter3 -1
print(str1 + str2 + str3)
LAST SLIDE
CHAPTER 4

You might also like