วันจันทร์ที่ 14 กันยายน พ.ศ. 2558
Lab 4.5 Python generic
Lab 4.5 Python generic 1 (Sum 1 to N )
def setup():
N = 10
print("N = ",N)
sum_1_to_N(N)
def sum_1_to_N(n):
sum_num = 0
while(n>0):
if(n>1):
print(n,"+",end="")
else:
print("1 = ",end="")
sum_num = sum_num + n
n = n - 1
print(sum_num)
print("Sum 1 to N = ",sum_num)
setup()
Lab 4.5 Python generic 2 (Grade Calculation Program )
def setup():
score = 80
grade_Calculate(score)
def grade_Calculate(score):
print("*******Grade Calculation Program*******")
print("score = ",score);
print("Grade : ",end = "")
if(score >= 80):
print("A")
else:
if(score >= 70):
print("B")
else:
if(score >= 60):
print("C")
else:
if(score >= 45):
print("D")
else:
print("F")
print("***************************************")
setup()
Lab 4.5 Python generic 3 (Power Of Ten)
def setup():
number = 2*pow(10,12)
power_of_ten(number)
def power_of_ten(number):
print("*************Power of ten**************");
print("Value = ",number);
print("Power of 10 = ",end = "");
if(number >= 1.0e30):
print("30")
print("Number : Nonillion")
else:
if(number >= 1.0e21):
print("21")
print("Number : Sextillion")
else:
if(number >= 1.0e18):
print("18")
print("Number : Quintillion")
else:
if(number >= 1.0e15):
print("15")
print("Number : Quadrillion")
else:
if(number >= 1.0e12):
print("12")
print("Number : Trillion")
else:
if(number >= 1.0e9):
print("9")
print("Number : Billion")
else:
if(number >= 1.0e6):
print("6")
print("Number : Million")
print("**************************************")
setup()
Lab 4.5 Python generic 4 (Leap Year)
def setup():
year = 1952
leap_year(year)
def leap_year(year):
print("*************Leap Year*****************")
print("Year = ",year)
if((year%4 == 0 and (year%100 != 0) ) or (year%400 == 0) ):
print("Leap Year")
else:
print("Not a Leap Year")
print("***************************************")
setup()
Lab 4.5 Python generic 5 (Delivery Charge)
def setup():
typePackage = 1
typeService = 2
weight = 6.00
delivery_charge(typePackage,typeService,weight)
def delivery_charge(typePackage,typeService,weight):
cost = 0.00
errorCost = False
print("*************Expressimo Delivery Service*****************")
print("*******************Delivery Charge***********************")
print(" Type Of Package | Type Of Service ")
print(" 1) Letter (1) | 1) Next Day Priority (1) ")
print(" 2) Box (2) | 2) Next Day Standard (2) ")
print(" | 3) 2-Day (3) ")
print("")
if(typePackage == 1):
print("Type Of Package : Letter")
elif(typePackage == 2):
print("Type Of Package : Box")
else:
print("Type Of Package : error")
if(typeService == 1):
print("Type Of Service : Next Day Priority")
elif(typeService == 2):
print("Type Of Service : Next Day Standard")
elif(typeService == 3):
print("Type Of Service : 2-Day")
else:
print("Type Of Service : error")
print("Weight : ",weight,end = "");
if(typePackage == 1):
print(" oz")
if(typeService == 1):
cost = 12.00
if(typeService == 2):
cost = 10.50
if(typeService == 3 or weight > 8.00):
errorCost = True
if(typePackage == 2):
print(" pound")
if(typeService == 1):
if(weight <= 1):
cost = 15.75
else:
cost = 15.75+((weight-1)*1.25)
if(typeService == 2):
if(weight <= 1):
cost = 13.75
else:
cost = 13.75+((weight-1)*1.00)
if(typeService == 3):
if(weight <= 1):
cost = 7.00
else:
cost = 7.00+((weight-1)*0.5)
if(errorCost):
print("Cost : Service not alivable")
else:
print("Cost : $",cost)
print("*********************************************************")
setup()
Lab 4.5 Python generic 6 (BMI calculator)
def setup():
bmi_cal(65,170)
def bmi_cal(Weight,Height):
bmi = 0
bmi = Weight/(pow(Height/100,2))
print("***BMI calculator***");
print("Weight = ",Weight," Kg")
print("Height = ",Height," cm")
print("BMI = ",bmi)
setup()
Lab 4.5 Python generic 7 (Circumference and area of a circle calculation program)
PI = 3.1415926535897932384626433832795
def setup():
cal_circle(8.00)
def cal_circle(Diameter):
circum = 0
area = 0
circum = 2*PI*(Diameter/2)
area = PI*(pow((Diameter/2),2))
print("***Circumference and area of a circle calculation program***")
print("Diameter = ",Diameter," m")
print("Circumference = ",circum," m")
print("Area = ",area," m^2")
setup()
Lab 4.5 Python Generic 8 ( Multiplication table )
def setup():
start_num = 12
end_num = 15
draw_multiplication_table(start_num,end_num)
def draw_multiplication_table(current,end_num):
n = 1
result = 0
while(current <= end_num):
print(" *",current,"* ")
while(n <= 12):
result = current*n
print(current,"X",n," = ",result)
n = n + 1
print(" ")
n = 1
current = current + 1
setup()
Lab 4.5 Python Generic 9 ( Sum of prime numbers from 1 to N )
def setup():
end_num = 100
sum_prime_num(end_num)
def sum_prime_num(end_num):
current = 2
sum = 0
n = 1
print("Prime number : ",end = "")
while(current<=end_num):
ref_num = 2
prime_num = True
while(ref_num<current and prime_num):
if(current%ref_num==0):
prime_num = False
ref_num = ref_num + 1
if(prime_num):
sum = sum + current
print(current," ",end ="")
if(n%15 == 0):
print(" ")
n = n + 1
current = current + 1
print(" ")
print("Sum of Prime number = ",sum)
setup()
Lab 4.5 Python Generic 10 (Loan payment)
def setup():
loan = 5000
interestRate = 12
monthsTerm = 12
loan_cal(loan,interestRate,monthsTerm)
def loan_cal(loan,interestRate,monthsTerm):
monthlyPayment = 0
effectiveInterest = 0
interest = 0
principal = 0
Balance = 0
n = 1
term = 1
totalInterest = 0
effectiveInterest = interestRate/(100*12)
monthlyPayment = loan*(effectiveInterest/(1-pow(1+effectiveInterest,-monthsTerm)))
Balance = loan
print("Loan = ",loan)
print("Interest rate = ",interestRate,"%")
print("Loan term = ",monthsTerm," mounth(s) (",monthsTerm/12," year(s))")
print("Payment per mounth = ",monthlyPayment)
print("Payment No. : Interest : Principal : Unpaid Balance : Total Interest to Date")
while(term<=monthsTerm):
interest = effectiveInterest*Balance
principal = monthlyPayment-interest
Balance = Balance-principal
totalInterest = totalInterest+interest
print(" ",round(term)," ",end = "")
print(round(interest)," ",round(principal)," ",end ="")
print(round(Balance)," ",round(totalInterest))
term = term + 1
if(n%12 == 0):
print(" ")
n = n + 1
setup()
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น