Chapter 2: Using C++ Operators

C8NEG :Page 166

In [1]:
temp=-12
#Result
print -temp
12

C8DIV :Page 167

In [1]:
#To compute weekly pay
#Get input
#yearly=input("What is your annual pay?")
yearly=38000.00
weekly=yearly/52
#Result
print "\nYour weekly pay is ",float(weekly)
Your weekly pay is  730.769230769

C8AVG1 :Page 172

In [2]:
#Compute the average of three grades
grade1=87.5
grade2=92.4
grade3=79.6
#Average calculation
avg=grade1+grade2+grade3/3.0
#Result
print "The average is: ",avg
The average is:  206.433333333

C8DATA :Page 179

In [3]:
#Declaration
bonus=50
salary=1400.50
#Calculation
total=salary+bonus
#Result
print "The total is ","%.2f" %total
The total is  1450.50

C8INT1 :Page 181

In [23]:
#Calculate interest
days=45
principle=3500.00
interest_rate=0.155
#daily interest rate
daily_interest=interest_rate/365          
daily_interest=principle*daily_interest*days
#Update principle
principle+=daily_interest                   
#Result
print "The balance you owe is:","%.2f" %principle
The balance you owe is: 3566.88

C9PAY1 :Page 193

In [1]:
#Calculate salesperson's pay based on his or her sales
print "Payroll Calculation\n"
print "------------------------\n"
#Get input
sal_name=raw_input("What is salesperson's last name? ")
hours=input("How many hours did the salesperson work? ")
total_sales=input("What were the total sales? ")
bonus=0
#Compute base pay
pay=4.10*float(hours)           
if total_sales>8500.00:
    bonus=500.00
#Result
print sal_name,"made $","%.2f" %pay, "\n and got a bonus of $","%.2f" %bonus
Payroll Calculation

------------------------

What is salesperson's last name? Harrison
How many hours did the salesperson work? 40
What were the total sales? 6050.64
Harrison made $ 164.00 
 and got a bonus of $ 0.00

C9AGE :Page 195

In [26]:
#Get input
age=input("What is the student's age?")
if age<10:
          print "\n*** The age cannot  be less than 10 ***\n"
          print "Try again...\n"
          age=input("What is the student's age?")
#Result
print "\nThank you. You entered a valid age."
What is the student's age?3

*** The age cannot  be less than 10 ***

Try again...

What is the student's age?21

Thank you. You entered a valid age.

C9SQR1 :Page 197

In [32]:
##Print square of the input value if it is lessthan 180
#Get input
num=input("What number do you want to see the square of?")
if num<=180:
    square=num*num
    print "The square of ",num,"is ",square,"\n"
    print "\nThank you for requesting square roots.\n"   
num=input("What number do you want to see the square of?")
if num>180:
    import os
    os.system('\a')
    print "\n* Square is not allowed for numbers over 180 *"
    print "\nRun this program again trying a smaller value."
print "\nThank you for requesting square roots.\n"
What number do you want to see the square of?45
The square of  45 is  2025 


Thank you for requesting square roots.

What number do you want to see the square of?212

* Square is not allowed for numbers over 180 *

Run this program again trying a smaller value.

Thank you for requesting square roots.

C9IFEL1 :Page 200

In [33]:
#Get input
#num=input("What is your answer?\n")
num=1
if num>0:
    print "\nMore than 0\n"
else:
    print "\nLess or equal to 0\n"

print "\nThanks for your time."
More than 0


Thanks for your time.

C9IFEL2 :Page 200

In [34]:
#Test user's first initial and prints a message.
#Get input
#last=raw_input("\nWhat is your last name?\n")
last="Praveen"
#test the initial
if last[0] <= 'P':
    print "Your name is early in the alphabet.\n"
else:
    print "You have to wait a while for Your name to be called\n"
Your name is early in the alphabet.

C9PAY2 :Page 201

In [36]:
#Get input
#hours=input("\nHow many hours were worked?")
#rate=input("\nWhat is the regular hourly pay?")
hours=44
rate=0.20
#Compute pay
if hours>50:
    dt=2.0*rate*float(hours-50)
    ht=1.5*rate*10.0
else:
    dt=0.0
#Time and a half
if hours>40:
    ht=1.5*rate*float(hours-40)
#Regular pay
if hours>=40:
    rp=40*rate
else:
    rp=float(hours)*rate
#Payroll
pay=dt+ht+rp        
#Result
print "\nThe pay is ","%.2f" %pay
The pay is  9.20

C9SERV :Page 202

In [37]:
#if...else...if
#yrs=input("\nHow many years of service?")
yrs=25
if yrs>20:
    print "\nGive a gold watch"
else:
    if yrs>10:
        print "\nGive a paper weight"
    else:
        print "\nGive a pat on the back"
Give a gold watch

C10YEAR :Page 212

In [41]:
#To determine if it is Summer Olympics year
#year=input("\nWhat is a year for the test?")
year=2004
#Test the Year
if year%4==0 and year%10==0:
    print "\nBoth Olympics and U.S. Census!"
    exit(0)
if year%4==0:
    print "\nSummer Olympics only"
else:
    if year%10==0:
        print "\nU.S. Census only"
Summer Olympics only

C10AGE :Page 213

In [1]:
#Get input
#age=input("\nWhat is your age?\n")
age=20
if age<10 or age>100:
    print "*** The age must be between 10 and 100 ***\n"
else:
    print "You entered a valid age."
You entered a valid age.

C10VIDEO :Page 214

In [5]:
# -*- coding: cp1252 -*-
#Program that computes video rental amounts and gives
# appropriate discounts based on the day or customer status.
print "\n *** Video Rental Computation ***\n"
print "--------------------------------------\n"
tape_charge=2.00        #Before-discount tape fee-per tape.
first_name=raw_input("\nWhat is customer's first name? ")
last_name=raw_input("\nWhat is customer's last name? ")
num_tapes=input("\nHow many tapes are being rented? ")
val_day=raw_input("Is this a Value day (Y/N)?")
sp_stat=raw_input("Is this a Special Status customer (Y/N)?")

# Calculate rental amount.
discount=0.0
if val_day=='Y' or sp_stat=='Y':
    discount=0.5
    x=num_tapes*tape_charge
    y=discount*num_tapes
    rental_amt=x-y
print "\n** Rental club **\n"
print first_name,last_name,"rented ",num_tapes," tapes "
print "The total was ","%.2f" %rental_amt
print "The discount was ","%.2f" %discount,"per tape\n"
if sp_stat=='Y':
    print "\nThank them for being a special Status customer\n"
 *** Video Rental Computation ***

--------------------------------------


What is customer's first name? Jerry

What is customer's last name? Parker

How many tapes are being rented? 3
Is this a Value day (Y/N)?Y
Is this a Special Status customer (Y/N)?Y

** Rental club **

Jerry Parker rented  3  tapes 
The total was  4.50
The discount was  0.50 per tape


Thank them for being a special Status customer

In [ ]: