Chapter 1: Introduction to C++

C3FIRST, Page number:52

In [1]:
#Variable Declaration
i=4
j=i+7
c='A'
x=9.087
x=x*4.5
#Result
print i,c,j,x
4 A 11 40.8915

C4ST1, Page number:85

In [2]:
#Result
print "C++ programming is fun!"
C++ programming is fun!

C4ST2, Page number:86

In [3]:
#Declaration
tax_rate=0.08
sale=22.54
#Calculation
tax=sale*tax_rate
#Result
print "The sales tax is :" ,tax
The sales tax is : 1.8032

C4AREAC,Page number:95

In [10]:
#Declaration
PI=3.14159
radius=5
#Calculation
area=radius*radius*PI
#Result
print "The area is ",area
radius=20
#Calculation
area=radius*radius*PI
#Result
print "The area is ",area
The area is  78.53975
The area is  1256.636

C5INIT,Page number:108

In [11]:
#Get input
#first=raw_input("Enter your first name:")
#last=raw_input("Enter your last name")
first="perry"
last="greg"
#Print the Initials
print "Your initials are ",first[0],last[0] 
Your initials are  p g

C6PRE,Page number:114

In [12]:
#Declaration
AGE=28
MESSAGE="Hello, world"
i=10
age=5
# 'AGE' is different from 'age'
i=i*AGE                                   
#Result
print i," ",age," ",AGE,"\n"
print MESSAGE                       
280   5   28 

Hello, world

C6INCL1,Page number:119

In [13]:
#Result
print "Kelly Jane Peterson\n"
print "Apartment #217\n"
print "4323 East Skelly Drive\n"
print "New York, New York\n"
print "             10012\n"
Kelly Jane Peterson

Apartment #217

4323 East Skelly Drive

New York, New York

             10012

C6INCL3,Page number:120

In [14]:
source = "This is fun!"
#source is copied to message
import copy
message=copy.copy(source)
#Result
print message
This is fun!

C6DEF1,Page number:121

In [2]:
MYNAME="Phil Ward"
name=MYNAME
#Result
print "My name is ",name,"\n"
print "My name is ",MYNAME,"\n"
My name is  Phil Ward 

My name is  Phil Ward 

C6DEF2,Page number:122

In [1]:
#function definition
def X4(b,c,d):
    return 2*b+c+3*b+c+b+c+4*b+c+b+c*c+b+c-d
b=2
c=3
d=4
e= X4 (b,c,d)
#Result
print e,",",b+c,",",b+c+b+c,",",b+c+b+c*c+b+c-d,",",X4(b,c,d)
44 , 5 , 10 , 17 , 44

C7PRNT1,Page number:136

In [5]:
#Declaration
first='E'
middle='W'
last='C'
age=32
dependents=2
salary=25000.00
bonus=575.25
#Result
print "Here are the initials: "
print first,middle,last
print "The age and number of dependents are "
print age," ",dependents
print "The salary and bonus are "
print salary," ",bonus
Here are the initials: 
E W C
The age and number of dependents are 
32   2
The salary and bonus are 
25000.0   575.25

C7TEAM, Page number:138

In [6]:
#table of team names and hits for three weeks
print "Parrots\tRams\tKings\tTitans\tChargers"
print "3\t5\t3\t1\t0"
print "2\t5\t1\t0\t1"
print "2\t6\t4\t3\t0"
Parrots	Rams	Kings	Titans	Chargers
3	5	3	1	0
2	5	1	0	1
2	6	4	3	0

C7PAY1,Page number:141

In [7]:
# Computes and prints payroll data properly in dollars and cents.
emp_name="Larry Payton"
pay_date="03/09/92"
hours_worked=43
rate=7.75                       #pay per hour
tax_rate=.32                    #Tax percentage rate
#Compute the pay amount
gross_pay=hours_worked*rate
taxes=tax_rate*gross_pay
net_pay=gross_pay-taxes
#Results
print "As of: ",pay_date
print emp_name," worked ",hours_worked,"hours"
print "and got paid",round(gross_pay,2)
print "After taxes of: ",round(taxes,2)
print "his take-home pay was $",round(net_pay,2)
As of:  03/09/92
Larry Payton  worked  43 hours
and got paid 333.25
After taxes of:  106.64
his take-home pay was $ 226.61

C7SLTX1, Page number:146

In [10]:
#getting total sale as float number.
#print "What is the total amount of the sale?"
#total_sale=float(raw_input())   
total_sale=50
#Compute sales tax
stax=total_sale*0.07    
#Results
print "The sales tax for",float(round(total_sale,2)),"is",round(stax,2)
The sales tax for 50.0 is 3.5

C7PHON1, Page number:147

In [11]:
#request user's name and print it as it would appeat in a phone book
#get name
#first=raw_input("What is your first name?\n")
#last=raw_input("What is your last name?\n")
first="perry"
last="greg"
print "\n\n"
print "In a phone book,your name would look like this :\n"
print last,",",first 


In a phone book,your name would look like this :

greg , perry

C7MATH, Page number:148

In [1]:
#Simple Addition
print "*** Math Practice ***\n"
#num1=input("What is the first number:")
#num2=input("What is the second number:")
num1=10
num2=20
ans=num1+num2
#get user answer
#her_ans=input("\nWhat do you think is the answer?")
her_ans=30
#Result
print "\n",num1,"plus",num2,"is",ans,"\n\nHope you got it right!"
*** Math Practice ***


10 plus 20 is 30 

Hope you got it right!

C7PS2, Page number:150

In [13]:
message="Please turn on your printer."
#Result
print message
Please turn on your printer.

C7PRNTF, Page number:153

In [15]:
#Declaration
first='E'
middle='W'
last='C'
age=32
dependents=2
salary=25000.00
bonus=575.25
#Result
print "Here are the initials: "
print first," ",middle," ",last,"\n"
print "The age and number of dependents are: "
print age," ",dependents,"\n"
print "The salary and bonus are: "
print "%.6f" %salary," ","%.6f" %bonus
Here are the initials: 
E   W   C 

The age and number of dependents are: 
32   2 

The salary and bonus are: 
25000.000000   575.250000

C7SLTXS, Page number:156

In [18]:
#prompt for a sales amount and print sales tax
#getting total sale as float number
#print "What is the total amount of the sale?"
#total_sale=float(raw_input())   
total_sale=10
#compute sales tax
stax=total_sale*0.07    
#Result
print "The sales tax for",float(round(total_sale,3)),"is",round(stax,3)
The sales tax for 10.0 is 0.7
In [ ]: