Chapter 1: Elementary C++ Programming

Example 1.1, page no. 2

In [1]:
print "Hello, World!\n"
Hello, World!

Example 1.2, page no. 3

In [2]:
print "Hello, World!\n"
Hello, World!

Example 1.3, page no. 4

In [3]:
print "Hel" + "lo, Wo" + "rld!" 
Hello, World!

Example 1.4, page no. 5

In [4]:
print  "Hello, W" + 'o' + "rld" + '!' 
Hello, World!

Example 1.5, page no. 5

In [5]:
print  "The Millennium ends Dec %d %d " %(31,2000)
The Millennium ends Dec 31 2000 

Example 1.6, page no. 5

In [6]:
m = 44 # assigns the value 44 to the variable m
print "m = %d " % m,
n = m + 33 # assigns the value 77 to the variable n
print "and n = %d " % n
m = 44  and n = 77 

Example 1.7, page no. 6

In [7]:
n=44
print "n =  %d" % n
n =  44

Example 1.8, page no. 7

In [8]:
# Python does not have semicolons so wont give any errors.
n=44
print "n = %d" % n 
n = 44

Example 1.9, page no. 7

In [9]:
m = 0 #In python we do not have declaration of variables, we just initialize it and use it.
n=44
print "m =  %d  and n = %d" %(m,n)
m =  0  and n = 44

Example 1.10, page no. 8

In [2]:
BEEP = '\b'
MAXINT = 2147483647
N = MAXINT/2
KM_PER_MI = 1.60934
PI = 3.14159265358979323846

Example 1.11, page no. 8

In [11]:
print "Enter two integers: "
m = int(raw_input())
n = int(raw_input())
print "m = %d , n = %d " %(m,n)

print "Enter three decimal numbers: "
x = float(raw_input())
y = float(raw_input())
z = float(raw_input())

print "x = %f , y =  %f , z = %f" %(x,y,z)

print "Enter four characters: ";
c1 = raw_input()
c2 = raw_input()
c3 = raw_input()
c4 = raw_input()
print "c1 = " + c1 + ", c2 = " + c2 + ", c3 = " + c3 + ", c4 = " + c4 
Enter two integers: 
22
44
m = 22 , n = 44 
Enter three decimal numbers: 
2.2
4.4
6.6
x = 2.200000 , y =  4.400000 , z = 6.600000
Enter four characters: 
A
B
C
D
c1 = A, c2 = B, c3 = C, c4 = D