Chapter 2: An Overview of C++

Example 2.1, Page Number: 12

In [1]:
print "This is my first C++ program."
This is my first C++ program.

Example 2.2, Page Number: 17

In [2]:
 
x=None              

x=1023    #this assigns 1023 to x

#Result
print "This program prints the value of x: ",x    #prints x,i.e, 1023
This program prints the value of x:  1023

Example 2.3, Page Number: 18

In [3]:
#Variable declaration
gallons=10     #User input
liters=None

liters=gallons*4  #convert to liters

#Result
print "Liters: ",liters
Liters:  40

Example 2.4, Page Number: 20

In [4]:
#Variable declaration
gallons=10.20     #User input
liters=None

liters=gallons*3.7854  #convert to liters

#Result
print "Liters: ",liters
Liters:  38.61108

Example 2.5, Page Number: 21

In [5]:
 
def myfunc():
    print "Inside myfunc() "
  
print "In main()"
myfunc()            #call myfunc()
print "Back in main()"
In main()
Inside myfunc() 
Back in main()

Example 2.6, Page Number: 22

In [6]:
 
print abs(-10)
10

Example 2.7, Page Number: 23

In [1]:
 
def mul(x,y):
    print x*y,

#calling mul
mul(10,20)
mul(5,6)
mul(8,9)
200 30 72

Example 2.8, Page Number: 24

In [8]:
 
def mul(x,y):
    return x*y         #return product of x and y

#Variable declaration
answer=mul(10,11)    #assign return values

#Result
print "The answer is: ",answer
The answer is:  110

Example 2.9, Page Number: 26

In [9]:
print "one"
print "two"           #prints in different line
print "three","four"  #prints all in same line
one
two
three four

Example 2.10, Page Number: 27

In [10]:
 
#Variable declaration
a=10                     #user input for two numbers
b=20

#Result
if a<b:
    print "First number is less than second. "
First number is less than second. 

Example 2.11, Page Number: 28

In [3]:
for count in range(1,100+1):
    print count,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Example 2.12, Page Number: 30

In [12]:
 
a=10                         #User input for two numbers
b=20

#Result
if a<b:
    print "First number is less than second"
    print "Their difference is: ",b-a
First number is less than second
Their difference is:  10
In [ ]: