Progrestest 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

NAME: Vo Thai Hoang Anh

QE : 170215
Class: AI17C

Q.1. (2.5 marks)


Print the sum of the current number and the previous number
Write a program to iterate the first 10 numbers and in each iteration,
print the sum of the current and previous number.

Code Insert\Scrennshot

#program to iterate the first 10


numbers and in each iteration,
print the sum of the current and
previous number.
print ( "Printing current and
previous number and their sum
in a range(10) ")
previous_num=0
#create a variable previous_num
and assign it to 0
for num in range(1,11):
#repeat the first 10 numbers
sum_of_them = previous_num
+ num
# sum of the current number and
the previous number
print ( " Current number ",
num , " Previous number ",
previous_num , " Sum " ,
sum_of_them )
previous_num = num
#change the value previous
number to current number

Q.2. (2.5 marks)


If a, b, c are sides of a triangle, then calculate its perimeter and area,
otherwise display on the screen a message "a, b, c is not side of a
triangle". The area is calculated by the Heron formula below:
(a+ b+c )
p=
S1 = √ p( p−a)( p−b)( p−c) , where 2

Insert\Scrennshot
Code
import math
a = float(input("Enter a:"))
b = float(input(" Enter b:"))
c = float(input(" Enter c:"))
#because real numbers, so
we using float
if(((a+b)>c) and ((a+c)>b)
and ((b+c)>a)) :
#check if it is a triangle or
not
perimeter = (a+b+c)
#perimeter of triangle
p=(a+b+c)/2
#half perimeter of triangle
The_area_of_triangle=
""
#create a variable that
stores the result as empty
The_area_of_triangle=
math.sqrt(p*(p-a)*(p-b)*(p-
c))
#Using Heron formula
print(" the perimeter
of triangle is : ", perimeter)
print("the area of
triangle
is:",The_area_of_triangle)
#Display the area of
triangle on the screen
else:
print("a, b, c are not
side of a triangle ")

Q.3. (2.5 marks)


Assign a different name to function and call it through the new name

Output:
Insert\Scrennshot
Code
#program assign a different
name to function and call it
through the new name
def display_student(name,
age):
#create function with 2
variable : name , age
print(name, age)
#call first name
display_student("Minh", 100)
show_student =
display_student
show_student("Quang", 19)

Q.4. (2.5 marks)


Write a program that performs the following tasks:
1. Input an integer number n, where n > 5 (If n≤ 5 then prompt a user
to re-enter). Then calculate
2. S1 = 1 + 2 + 3 + ... + n.
3. S2 = n!
1 1 1
4. S3 =1+ 2 + 3 +...+ n .
5. Re-input n. Check whether n is a prime number or not.

Insert\Scrennshot
Code
n = int(input(" Please enter
n:"))
S1=0
S2=1
S3=0
if n<=5:
print ( " Re-enter n>5")
else:
for i in range ( 1, n+1 ):
S1= S1 + i # calculate S1
= 1 + 2 + 3 + ... + n.

S2=S2*i #3.calculate S2 =
n!

S3=S3 + 1/i #calculate S3


=1+1/2+1/3+...+1/n

print ( " Sum from 1 to


n=",S1)
print ( " Result of n!=", S2)
print ( " Result of S3=", S3)
def check_prime_number (
n ): #create
check_prime_number
function to check n is a
prime number or not
check = 0 #assign check =
0 , if check = 0 is false, check
= 1 is true.
if ( n < 2 ):
check = 0 #because
prime number always >=2
else:
for i in range ( 2 , n +
1):
if ( n % i == 0 ):
check = 0
else:
check = 1
return check
n = int(input("Re-input
n:"))
result =
check_prime_number(n)
#recall check_prime_number
if result == 1: #result = 1
its mean condition is True
print ( " n is prime
number")
else:
print ( " n is not prime
number ")

You might also like