กันยายน 20, 2558

Lab4x_Sum-of-1-to-N & Multiplication-Table & Sum-of-Prime-Number & Monthly-Loan-Payment

# Sum Of 1 to N
def setup():
   n = 15
   sumOfOneToN(n)
def sumOfOneToN(n):
   sum = 0
   print("N = ",n)
   while(n>0):
      sum += n
      n -= 1
   print("sum of int from 1 to N = ",sum)
setup()



# Multiplication Table
def setup():
   num = 13
   multipli_table(num)
def multipli_table(n):
   multi = 1
   print("Multiplication-Table of ",n)
   while(multi <= 12):
      print(n," x ",multi," = ",(n*multi))
      multi += 1
setup()



# Sum of Prime Number
def setup():
   final_num = 100
   primeCal(final_num)
def primeCal(n):
   result = 0
   num = 1
   print("Sum of Prime number from 1 to",n)
   while(num<=n):
      primeNum = True
      i = 2
      while(i<num and primeNum):
         if((num%i) == 0):
            primeNum = False
         i += 1
      if(primeNum):
         result += num
      num += 1
   print("result =",result)
setup()



# Monthly loan payment
def setup():
   loan = 5000
   interest_rate = 12  # percent
   loan_term = 12      # months
   loanCal(loan, interest_rate, loan_term)

def loanCal(loan, interest_rate, loan_term):
   balance = loan
   total_interest = 0
   count=1
   j = interest_rate/100/12
   payMonthly = loan*(j/(1-(pow(1+j,-loan_term))))
   print("***** Monthly Loan Payment *****")
   print(" Loan Amount : $",loan)
   print(" Loan Term   : ",loan_term," months")
   print(" Interest Rate : ",interest_rate,"%")
   print("************************************")
 
   print("Payment NO.|   Balance   |   Interest   | Principal | Unpaid Balance | Total Interest to Date")
   while (count<=loan_term):
      interest = j*balance
      principal = payMonthly-interest
      print("     ",round(count),"       ",round(balance),"         ",round(interest),"         ",round(principal), end="")
      balance -= principal
      total_interest += interest
      print("          ",round(balance),"            ",round(total_interest))
      count += 1
   print("Payment Every Month : $",payMonthly)
   print("Total of 12 Payments : $",(loan+total_interest))
   print("Total Interest : $",total_interest)
setup()

Lab4x_Grade-Calculation & Leap-Year & Power-of-Ten & Delivery-Charge

# Grade Calculation
def setup():
   score = 60
   gradeCal(score)
def gradeCal(s):
   print("Score = ",s)
   if(s>=80 and s<=100):
      print("Grade : A")
   elif(s>=70 and s<80):
      print("Grade : B")
   elif(s>=55 and s<70):
      print("Grade : C")
   elif(s>=40 and s<55):
      print("Grade : D")
   elif(s>=0 and s<40):
      print("Grade : F")
   else:
      println("Grade : -")
setup()



# Leap Year
def setup():
   year = 1800
   leapYear(year)
def leapYear(y):
   print("Year = ",y);
   if(y%400==0 or (y%4==0 and y%100!=0)):
      print("Leap Year")
   else:
      print("Not Leap Year")
setup()



# Power of ten
def setup():
   powerTen = 29
   powerOfTen(powerTen)
def powerOfTen(p):
   print("Power-of-ten = ",p)
   if(p>=100):
      if(p>100):
         print("Number = Googol++")
      else:
         print("Number = Googol")
   elif(p>=30):
      if(p>30):
         print("Number = Nonillion++")
      else:
         print("Number = Nonillion")
   elif(p>=21):
      if(p>21):
         print("Number = Sextillion++")
      else:
         print("Number = Sextillion")
   elif(p>=18):
      if(p>18):
         print("Number = Quintillion++")
      else:
         print("Number = Quintillion")
   elif(p>=15):
      if(p>15):
         print("Number = Quadrillion++")
      else:
         print("Number = Quadrillion")
   elif(p>=12):
      if(p>12):
         print("Number = Trillion++")
      else:
         print("Number = Trillion")
   elif(p>=9):
      if(p>9):
         print("Number = Billion++")
      else:
         print("Number = Billion")
   elif(p>=6):
      if(p>6):
         print("Number = Million++")
      else:
         print("Number = Million")
   else:
      print("Number = --")
setup()



# Delivery Charge
def setup():
   packaging = 2  # (1)letter
                  # (2)box
   service = 1    # (1)Next-Day-Priority
                  # (2)Next-Day-Standard
                  # (3)2-Day
   weight = 11    # oz unit for letter
                  # pound unit for box
   error = False

   if(packaging == 1):
      print("type of packaging = letter")
   elif(packaging == 2):
      print("type of packaging = box")
   else:
      print("type of packaging = Error")
      error = True

   if(service == 1):
      print("service = Next-Day-Priority")
   elif(service == 2):
      print("service = Next-Day-Standard")
   elif(service == 3):
      print("service = 2-Day")
   else:
      print("service = Error")
      error = True

   if(packaging == 1):
      print("Weight = ",weight," oz")
   elif(packaging == 2):
      print("Weight = ",weight," pound")

   #Calculation
   if(error == True):
      print("The charge = Error")
   elif(packaging == 1):
      if(service == 1):
         if(weight <= 8):
            print("The charge = $12.00")
         else:
            print("Error : Over Load")
      elif(service == 2):
         if(weight <= 8):
            print("The charge = $10.50")
         else:
            print("Error : Over Load")
      else:
         print("This service is not available")
   else:
      if(service == 1):
         value = 15.75+((weight-1)*1.25)
      elif(service == 2):
         value = 13.75+((weight-1)*1.00)
      else:
         value = 7.00+((weight-1)*0.50)
      print("The charge = $",value)

setup()

Lab4x_BMI & Circle

#Calculate body mass index (BMI)
def setup():
   weight= 60
   tall= 175
   BMI_cal(weight,tall)

def BMI_cal(weight,tall):
   BMI=weight/((tall/100)*(tall/100))
   print("Weight = " , weight)
   print("Height = " , tall)
   print("BMI = " , BMI)
setup()



#Calculate circumference and area of a circle from its diameter
def setup():
   diam=10
   circle_cal(diam)
def circle_cal(d):
   pi=22/7
   circum = 2*pi*(d/2)
   areaCir=pi*(d/2)*(d/2)
   print("Diameter = " , d)
   print("Circumference = " , circum)
   print("area of a circle = " , areaCir)
setup()

กันยายน 15, 2558

Lab4_Generic exercise

//*****sum-Of-1-to-N*****
void setup(){
  int N = 15;
  sumOfOneToN(N);
}
void sumOfOneToN(int n){
  int sum = 0;
  println("N = "+n);
  while(n>0){
    sum += n;
    n--;
  }
  println("sum of int from 1 to N = "+sum);
}

//*****Multiplication-Table*****
void setup(){
  int num = 13;
  multipli_table(num);
}
void multipli_table(int n){
  int multi = 1;
  println("Multiplication-Table of "+n);
  while(multi <= 12){
    println(n+" x "+multi+" = "+(n*multi));
    multi++;
    }
}

//*****Prime-Number*****
void setup(){
  int final_num = 100;
  primeCal(final_num);
}
void primeCal(int n){
  int num = 1;
  println("Prime number from 1 to "+n);
  while(num<=n){
    boolean primeNum = true;
    int i = 2;
    while(i<num && primeNum){
      if((num%i) == 0)
        primeNum = false;
      i++;
    }
    if(primeNum)
      print(num+" ");
    num++;
  }
}
//*****Monthly-loan-payment*****
void setup() {
  float loan = 5000;
  float interest_rate = 12; //percent
  int loan_term = 12; //months
  loanCal(loan, interest_rate, loan_term);
}

void loanCal(float loan, float interest_rate, float loan_term) {
  float payMonthly, balance = loan, total_interest=0;
  float interest, principal, j;
  int count=1;
  j = interest_rate/100/12;
  payMonthly = loan*(j/(1-(pow(1+j,-loan_term))));
  println("***** Monthly Loan Payment *****");
  println(" Loan Amount : $"+loan);
  println(" Loan Term   : "+loan_term+" months");
  println(" Interest Rate : "+interest_rate+"%");
  println("************************************");
  
  println("Payment NO.|   Balance   |   Interest   | Principal | Unpaid Balance | Total Interest to Date");
  while (count<=loan_term) {
    interest = j*balance;
    principal = payMonthly-interest;
    print("     "+count+"       "+balance+"      "+interest+"         "+principal);
    balance -= principal;
    total_interest += interest;
    println("       "+balance+"          "+total_interest);
    count++;
  }
  println("Payment Every Month : $"+payMonthly);
  println("Total of 12 Payments : $"+(loan+total_interest));
  println("Total Interest : $"+total_interest);
}

Lab4_songs

int limit=0;
int light_brown=#F7DEA7;
int white=255;
color screen;
color Line=255;
boolean dropNote=false;
int noteY=0;
void draw() {
  int noteX=10;
  size(350, 185);
  background(screen);
  limit++;
  limit %= 180;
  draw_song();
  if (dropNote) {
    while (noteX<width) {
      note(noteX);
      noteX += 25;
    }
      if (noteY>200) {
        dropNote=false;
        noteY=0;
   
    }
  }
}
void draw_song() {
  int posX=100;
  posX+=limit;
  noStroke();
  //draw"THE"
  fill(Line);
  rect(posX-25, 10, 70, 30); //blank"THE"
  fill(screen);
  rect(posX-25, 15, 7, 25);
  rect(posX-13, 15, 7, 25); //T
  rect(posX, 10, 15, 12);
  rect(posX, 27, 15, 13); //H
  rect(posX+20, 10, 3, 30);
  rect(posX+28, 15, 17, 7);
  rect(posX+28, 27, 17, 8); //E
  triangle(posX+33, 10, posX+45, 45, posX+45, 10);
  //draw"SCRIPT"
  fill(Line);
  arc(45, 75, 60, 60, HALF_PI, TWO_PI);
  arc(45, 131, 70, 70, HALF_PI+PI, TWO_PI+HALF_PI); //S
  arc(130, 105, 110, 110, QUARTER_PI+PI/32, TWO_PI-QUARTER_PI-PI/32); //C
  rect(165, 50, 20, 10);
  rect(165, 105, 20, 10);
  arc(185, 82.5, 60, 65, 3*PI/2, 5*PI/2);
  quad(170, 115, 185, 115, 215, 147, 215, 163); //R
  rect(220, 50, 10, 120); //I
  rect(235, 50, 10, 120);
  rect(245, 50, 20, 10);
  rect(245, 105, 20, 10);
  arc(265, 82.5, 60, 65, 3*PI/2, 5*PI/2); //P
  quad(265, 50, 330, 50, 335, 60, 265, 60);
  rect(300, 50, 10, 120); //T
  fill(screen);
  arc(45, 76, 40, 40, HALF_PI, TWO_PI);
  arc(45, 131, 50, 50, HALF_PI+PI, TWO_PI+HALF_PI); //S
  arc(130, 105, 90, 90, QUARTER_PI, TWO_PI-QUARTER_PI); //C
  triangle(147, 50, 155, 80, 155, 50);
  arc(185, 82.5, 40, 45, 3*PI/2, 5*PI/2); //R
  quad(220, 65, 230, 60, 230, 65, 220, 70); //I
  arc(265, 82.5, 40, 45, 3*PI/2, 5*PI/2); //P
  triangle(220, 170, 310, 130, 310, 170);
  fill(Line);
  rect(155, 50, 10, 97); //R
}
void note(int noteX) {
  fill(Line);
  ellipse(noteX, noteY, 15, 15);
  stroke(Line);
  strokeWeight(3);
  line(noteX+6, noteY, noteX+6, noteY-35);
  line(noteX+6, noteY-30, noteX+13, noteY-10);
  line(noteX+13, noteY-10, noteX+12, noteY-5);
  noteY++;
}
void mouseClicked() {
  if (mouseButton==RIGHT) {
    white *= -1;
    Line += white;
    screen += light_brown;
    light_brown*=-1;
  } else if (mouseButton==LEFT) {
    dropNote=true;
  }
}