Chapter 2: C++ Programming Basics

Example 2.1, Page Number: 36

In [1]:
print 'Every age has a language of its own\n'     #print the statement
Every age has a language of its own

Example 2.2, Page Number: 38

In [2]:
var1 = 20       #define var1 and assign value to it
var2 = var1 + 10      #assign value to var2

print 'var1+10 is',    #output text
print var2, '\n'      #output value of var2
var1+10 is 30 

Example 2.3, Page Number: 44

In [3]:
charvar1 = 'A'       #define a character type variable
charvar2 = '\t'      #define a character variable as tab

print charvar1,      #display character
print charvar2,      #display character

charvar1 = 'B'       #set character variable to character constant
print charvar1,      #display character

print '\n'
A 	B 

Example 2.4, Page Number: 46

In [4]:
ftemp = input("Enter temperature in fahrenheit: ")      #take temperature in fahrenheit from user

ctemp = (ftemp-32) * 5 / 9                              #calculating temprature in Celsius

print 'Equivalent in Celsius is:', ctemp, '\n'          #deisplay temprature in Celsius
Enter temperature in fahrenheit: 212
Equivalent in Celsius is: 100 

Example 2.5, Page Number: 48

In [5]:
PI = 3.14159                                      #float type variable

rad  = input("Enter radius of circle: ")          #get radius from user

area = PI * rad * rad                             #find area of circle

print 'Area is',area,'\n'                         #display area
Enter radius of circle: 0.5
Area is 0.7853975 

Example 2.6, Page Number: 52

In [6]:
#define the variables
pop1 = 2425785
pop2 = 47
pop3 = 9761

print 'LOCATION POP.\nPortcity',pop1,'\nHightown',pop2,'\nLowvilla',pop3,'\n'       #display all variables
LOCATION POP.
Portcity 2425785 
Hightown 47 
Lowvilla 9761 

Example 2.7, Page Number: 53

In [7]:
#define the variables
pop1 = 2425785
pop2 = 47
pop3 = 9761

print 'LOCATION %12s \nPortcity %12d \nHightown %12d \nLowville %12d \n' % ('POPULATION', pop1, pop2, pop3)
LOCATION   POPULATION 
Portcity      2425785 
Hightown           47 
Lowville         9761 

Example 2.8, Page Number: 55

In [8]:
from ctypes import c_int,c_uint

signedVar = c_int(1500000000)       #sigened
unsignVar = c_uint(1500000000)      #unsigned

signedVar.value = signedVar.value * 2
signedVar.value = signedVar.value / 3           #calculate exceeds range

unsignVar.value = (unsignVar.value * 2) / 3     #calculate within range

print 'signedVar =',signedVar.value          #wrong
print 'unsignVar =',unsignVar.value          #ok
signedVar = -431655766
unsignVar = 1000000000

Example 2.9, Page Number: 56

In [9]:
#define variables
count = 7
avgWeight = 155.5

totalWeight = count * avgWeight       #calculate total weight

print 'totalWeight =',totalWeight,'\n'       #print total weight
totalWeight = 1088.5 

Example 2.10, Page Number: 59

In [10]:
from ctypes import c_int

intVar = c_int(1500000000)

intVar.value = intVar.value * 10
intVar.value = intVar.value/10          #result too large
print 'intVar =',intVar.value,'\n',      #wrong answer

intVar = 1500000000

intVar = (intVar * 10) / 10;
print 'intVar =',intVar,'\n'           #right answer
intVar = 211509811 
intVar = 1500000000 

Example 2.11, Page Number: 61

In [11]:
print 6%8,'\n',7%8,'\n',8%8,'\n',9%8,'\n',10%8,'\n'
6 
7 
0 
1 
2 

Example 2.12, Page Number: 62

In [12]:
ans = 27            #define ans variable

ans += 10           #same as: ans = ans + 10
print ans,',',

ans -= 7           #same as: ans = ans - 7
print ans,',',

ans *= 2           #same as: ans = ans * 2
print ans,',',

ans /= 3           #same as: ans = ans / 3
print ans,',',

ans %= 3           #same as: ans = ans % 3
print ans,'\n'
37 , 30 , 60 , 20 , 2 

Example 2.13, Page Number: 64

In [13]:
#The ++ operator is not available in Python

count = 10

print 'count =',count
count += 1                #work as prefix

print 'count =',count
print 'count =',count
print 'count =',count

count += 1                #work as postfix
print 'count =',count
count = 10
count = 11
count = 11
count = 11
count = 12

Example 2.14, Page Number: 65

In [14]:
from math import sqrt                       #import for srt()

number = input("Enter the number: ")        #get the number

answer = sqrt(number)                       #find square root

print 'Square root is',answer,'\n'          #display it
Enter the number: 1000
Square root is 31.6227766017 

In [ ]: