Professional Documents
Culture Documents
IT Second Semester Chapter 4 Algorithms and Flowcharts - Updated
IT Second Semester Chapter 4 Algorithms and Flowcharts - Updated
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
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.
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
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int answer;
}
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
OUTPUT:
Word Example 4.12
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.
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.
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.✔
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.
OUTPUT:
Code1:
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
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