Algorithma
Algorithma
Student ID : 2312076
Class :B
Instructions : Based on what we have discussed on previous meeting about datatype, variable
and operator, please do this programming exercise using Visual Studio Code Python. Write
down your program and the rest of it using English. Submit the assignment with the
description, the code and the running output capture.
Task 1 - Write a program that inputs four integer values and prints the multiplication of them.
#Task 1
#Inputs four integer values and prints the multiplication of them.
Output Task 1:
Task 2 - Write a program that calculates and prints the area and the perimeter of a square
when the size of one side is given.
Answer:
# Task 2
# Input the size of one side of the square
side = float(input("Enter the length of one side of the square: "))
# Calculate Area
area = side**2
# Calculate Perimeter
perimeter = side*4
Output Task 2:
Task 3 - Write a program that, using a Python string, prints your name in the format shown
below after being prompted to input your first and the last name. Note that the last name
should come before the first name as shown.
Your full name is: last, first
Answer:
# Task 3
Output Task 3:
Task 4 - Write a program that prompts the user to enter two integers. It then prints their sum.
Run your program three (3) times, each with different values for variables.
Answer:
# Task 4
# Write a program that prompts the user to enter two integers.
# It then prints their sum. Run your program three (3) times, each with
different values for variables.
Output Task 4:
Task 5 – Locate the error or best replacement for this program below, show the output :
a) Program 1
result = x > y
print=(x, ">", y, "=", result)
result = x == y
print=(x, "==", y, "=", result)
result = x + x >=y
print(x, "==", y, "=", result)
Output Program 1:
Corrected Program 1:
x = int(input("Enter a number: ")) # the closing bracket on the word
“input” is moved to the end of the sentence
y = 4
result = x > y
print(x, ">", y, "=", result) # the equal sign after "print" is
ommited
result =x == y
print(x, "==", y, "=", result) # the equal sign after "print" is
ommited
result = x + x >=y
print(x, ">=", y, "=", result) # fixed the output massage
Output Corrected Program 1:
b) Program 2
Output Program 2:
Corrected Program 2:
print("Lets calculate the area of a circle!")
print("The formula to calculate the area is \nA = pi x r^2")
r = int(input("Enter the radius of a circle: "))
pi = 3.14
area = r * pi * (r ** 2) #the "x" sign replaced with "*", and the
sign "**" is use to calculate r2
print("The area of a circle is:", area)
Output Corrected Program 2:
c) Program 3
printf("")
printf("Hello, Welcome",
Name) printf("Your student ID
is", NIM)
printf("You are a student majoring in", Major)
Output Program 3:
Corrected Program 3:
print("Welcome to the class!") # Printf() should be fixed to
print()
name = input("Enter your name: ")
nim = input("Enter your student ID (2346xxx): ")
major = (input("Enter your major: "))
print("") # Printf() should be fixed to print()
print("Hello, Welcome", name) # Printf() should be fixed to print()
print("Your student ID is", nim) # Printf() should be fixed to
print()
print("You are a student majoring in", major) # Printf() should be
fixed to print()
d) Program 4
Output Program 4:
Corrected Program 4:
decnum = float(input("Enter a decimal number (e.g. 17.7): ")) #
float use to handle decimal number
negnum = int(input("Enter a negative number (e.g. -17): ")) # int
use to negative number
e) Program 5
print("enter the first
number:")
number1=(input())
prnt("enter the second
number:") number2=(input()
print("enter the third
number:")
number2=(input())
result=number1+number2+number3
print("addition of the three
integer:"result)
Output Program 5:
Corrected Program 5:
print("enter the first number:")
number1=int(input()) # add "int" before (input())
print("enter the second number:") # changed prnt() to print()
number2=int(input()) # add the parentheses at the end
print("enter the third number:")
number2=int(input()) # changed number 2 to number 3 to correct the
"third number"
result=number1+number2+number3
print("addition of the three integer:",result) # the sign used
before the word "result" is changed from "." to ","
f) Program 6
result=number1*number3*number3*number3
print("the multiplication of the four
integer:",resu1t)
Output Program 6:
Corrected Program 6:
print("Enter the first number: ")
number1 = int(input()) # add parentheses