Skip to content

Commit 01ca09f

Browse files
committed
Initial Commit
0 parents  commit 01ca09f

23 files changed

+593
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Fig. 2.1: fig02_01.py
2+
# Text printing program
3+
4+
print( "Welcome to Python!!\n" ) # Print a message on the screen
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Fig. 2.1: fig02_03.py
2+
# Printing a line of text with mulitple statements.
3+
4+
print( "Welcome " )
5+
print( "to Python!\n" )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Fig. 2.4: fig02_04.cpp
2+
# Printing multiple lines of text with a single statement.
3+
4+
print( "Welcome\nto\n\nC++!\n" )
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Fig. 2.5: fig02_05.cpp
2+
# Addition program that displays the sum of two integers.
3+
4+
# MAIN ==================================
5+
# function main begins program execution
6+
# =======================================
7+
8+
# variable declarations
9+
number1:int; # first integer to add
10+
number2:int; # second integer to add
11+
sum:int; # sum of number1 and number2
12+
13+
number1 = input( "Enter first integer: " ) # prompt user for data
14+
# read first integer from user into number1
15+
16+
number2 = input( "Enter second integer: " ) # prompt user for data
17+
# read second integer from user into number2
18+
19+
sum = int(number1) + int(number2) # add the numbers; store result in sum
20+
21+
22+
print( "Sum is %d\n" %sum ) # display sum
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Fig. 2.13: fig02_13.cpp
2+
# Comparing integers using if statements, relational operators
3+
# and equality operators.
4+
5+
6+
number1:int # first integer to compare
7+
number2:int # second integer to compare
8+
9+
# ===========================================================
10+
# WHY THIS WORKS
11+
# ===========================================================
12+
# The input prompts the user for two integers.
13+
#
14+
# When the user enters the value, they are a string "2 3"
15+
#
16+
# A string of "2 3" cannot be assigned to number1 and
17+
# number2 because the variables is declared using
18+
# Parallel Assignment.
19+
#
20+
# Since the input is a string "2 3", we can use string
21+
# object function to parse the information so that it can be
22+
# unpack and inserted into the Parallel Assignment.
23+
#
24+
# The split function splits the string 2 3 using the space
25+
# delimiter, turning the string 2 3 to a list ["2", "3"]
26+
#
27+
# Python will then automatically unpack the list into their
28+
# respective variable location.
29+
number1, number2 = input( "Enter two integers to compare: " ).split( )
30+
31+
if number1 == number2:
32+
print( "%s == %s" %(number1, number2) )
33+
34+
if number1 != number2:
35+
print( "%s != %s" %(number1, number2) )
36+
37+
if number1 < number2:
38+
print( "%s < %s" %(number1, number2) )
39+
40+
if number1 > number2:
41+
print( "%s > %s" %(number1, number2) )
42+
43+
if number1 <= number2:
44+
print( "%s <= %s" %(number1, number2) )
45+
46+
if number1 >= number2:
47+
print( "%s >= %s" %(number1, number2) )
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Fig. 3.1: fig03_01.py
2+
# Define class GradeBook with a member function displayMessage,
3+
# create a GradeBook object, and call its displayMessage function.
4+
5+
6+
# ==========================================
7+
# GradeBook class definition
8+
# ==========================================
9+
class GradeBook:
10+
11+
# ==============================================================
12+
# This is a member function that is responsible for the
13+
# functionality of the class.
14+
#
15+
# Noticed that this member function has "self" as its
16+
# first parameter.
17+
#
18+
# This is there to show Python that this is a member function
19+
# ==============================================================
20+
def displayMessage(self):
21+
print( "Welcome to the Grade Book!" )
22+
23+
24+
# ==========================================
25+
# funtion main begins program execution
26+
# ==========================================
27+
myBook:GradeBook = GradeBook( )
28+
myBook.displayMessage( )
29+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Fig. 3.1: fig03_01.py
2+
# Define class GradeBook with a member function displayMessage,
3+
# create a GradeBook object, and call its displayMessage function.
4+
5+
6+
# ==========================================
7+
# GradeBook class definition
8+
# ==========================================
9+
class GradeBook:
10+
11+
# ==============================================================
12+
# The declaration of variable is not essential
13+
# ==============================================================
14+
age:int
15+
16+
17+
# ==============================================================
18+
# This function is python's version of a constructor
19+
# Upon creation, this function will be called first.
20+
#
21+
# The 'self' parameter is self referential and is essential
22+
# to be listed as the first parameter.
23+
#
24+
# Any subsequent parameter listed is considered additional
25+
# information to initialize the starting state of the object.
26+
#
27+
# To set the initialize the variable age.
28+
# 'self' referential needs to be the object followed by the
29+
# variable name that you want to initialize.
30+
# ==============================================================
31+
def __init__(self, age):
32+
self.age = age
33+
34+
35+
# ==============================================================
36+
# This is a member function that is responsible in sharing
37+
# private information from the object.
38+
# ==============================================================
39+
def displayMessage(self):
40+
print( "Welcome to the Grade Book! " + str( self.age ) )
41+
42+
43+
# ==========================================
44+
# funtion main begins program execution
45+
# ==========================================
46+
myBook = GradeBook( 25 )
47+
myBook.displayMessage( )
48+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Fig. 3.3: fig03_03.py
2+
# Define class GradeBook with a member function that takes a parameter,
3+
# create a GradeBook object and call its displayMessage function.
4+
5+
class GradeBook:
6+
def displayMessage( self, courseName ):
7+
print( "Welcome to the grade book for \n" + courseName + "!\n" )
8+
9+
nameOfCourse:str = ""
10+
myGradeBook:GradeBook = GradeBook( )
11+
12+
print( "Please enter the course name: " )
13+
nameOfCourse = input( )
14+
print( )
15+
16+
myGradeBook.displayMessage( nameOfCourse )
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Question 2.16
3+
4+
(Arithmetic) Write a program that asks the user to enter two numbers, obtains the two
5+
numbers from the user and prints the sum, product, difference, and quotient of the two numbers.
6+
'''
7+
8+
a:int = 0
9+
b:int = 0
10+
11+
a, b = input( "Enter two integers: " ).split( )
12+
13+
print( "Sum is %d" %( int(a) + int(b) ) )
14+
print( "Difference is %d" %( int(a) - int(b) ) )
15+
print( "Product is %d" %( int(a) * int(b) ) )
16+
17+
if( int(b) != 0 ):
18+
print( "Quotient is %d" %( int(a) / int(b) ) )
19+
20+
if( int(b) == 0 ):
21+
print( "Cannot Divide by Zero" )
22+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Question 2.18
3+
4+
(Comparing Integers) Write a program that asks the user to enter two integers, obtains the
5+
numbers from the user, then prints the larger number followed by the words "is larger." If the
6+
numbers are equal, print the message "These numbers are equal."
7+
'''
8+
9+
a:int = 0
10+
b:int = 0
11+
12+
a, b = input( "Enter two integer for comparison: " ).split( )
13+
14+
a = int(a)
15+
b = int(b)
16+
17+
if( a > b ):
18+
print( "%d is larger than %d" %(a, b) )
19+
20+
if( b > a ):
21+
print( "%d is larger than %d" %(b, a) )
22+
23+
if( a == b ):
24+
print( "These numbers are equal" )
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Question 2.19
3+
4+
(Arithmetic, Smallest and Largest) Write a program that inputs three integers from the keyboard
5+
and prints the sum, average, product, smallest and largest of these numbers.
6+
'''
7+
8+
# get 3 inputs
9+
a:int = 0
10+
b:int = 0
11+
c:int = 0
12+
largest:int = 0
13+
smallest:int = 0
14+
15+
a, b, c = input( "Enter three integers: " ).split( )
16+
17+
a = int(a)
18+
b = int(b)
19+
c = int(c)
20+
21+
print( "Sum: %d" %( a + b + c ) )
22+
print( "Average: %d" %( (a + b + c) / 3 ) )
23+
print( "Product: %d" %( a * b * c ) )
24+
25+
26+
largest = a
27+
if( b > largest ):
28+
largest = b
29+
30+
if( c > largest ):
31+
largest = c
32+
33+
smallest = a
34+
if( b < smallest ):
35+
smallest = b
36+
if( c < smallest ):
37+
smallest = c
38+
39+
print( "Largest: %d" %(largest) )
40+
print( "Smallest: %d" %(smallest) )
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Question 2.20
3+
4+
(Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of
5+
a circle as an integer and prints the circle’s diameter, circumference and area. Use the constant value
6+
3.14159 for π. Do all calculations in output statements.
7+
'''
8+
9+
radius = int( input( "Enter a radius: " ) )
10+
11+
# Diameter (2r) Calculation
12+
print( "Diameter: %d" %( radius * 2 ) )
13+
14+
# Circumference (2πr) Calculation
15+
print( "Circumference: %d" %( 2 * 3.14159 * radius ) )
16+
17+
# Area (πr2) Calculation
18+
print( "Area %d" %( 3.14159 * ( radius * radius ) ) )
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Question 2.23
3+
4+
(Largest and Smallest Integers) Write a program that reads in five integers and determines
5+
and prints the largest and the smallest integers in the group. Use only the programming techniques
6+
you learned in this chapter.
7+
'''
8+
9+
a, b, c, d, e = input( "Enter 5 integers: " ).split( )
10+
11+
a = int( a )
12+
b = int( b )
13+
c = int( c )
14+
d = int( d )
15+
e = int( e )
16+
17+
largest = a
18+
if( b > largest ):
19+
largest = b
20+
if( c > largest ):
21+
largest = c
22+
if( d > largest ):
23+
largest = d
24+
if( e > largest ):
25+
largest = e
26+
27+
smallest = a
28+
if( b < smallest ):
29+
smallest = b
30+
if( c < smallest ):
31+
smallest = c
32+
if( d < smallest ):
33+
smallest = d
34+
if( e < smallest ):
35+
smallest = e
36+
37+
print( "Largest: %d" %( largest ) )
38+
print( "Smallest: %d" %( smallest ) )
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Question 2.24
3+
4+
(Odd or Even) Write a program that reads an integer and determines and prints whether
5+
it's odd or even. [Hint: Use the modulus operator. An even number is a multiple of two. Any multiple
6+
of two leaves a remainder of zero when divided by 2.]
7+
'''
8+
9+
a = int( input( "Enter an integer:" ) )
10+
11+
if( a % 2 == 0 ):
12+
print( "Even" )
13+
14+
if( a % 2 != 0 ):
15+
print( "Odd" )
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Question 2.25
3+
4+
(Multiples) Write a program that reads in two integers and determines and prints if the first
5+
is a multiple of the second. [Hint: Use the modulus operator.]
6+
'''
7+
8+
a, b = input( "Enter two integers: " ).split( )
9+
a = int( a )
10+
b = int( b )
11+
12+
if( a % b == 0 ):
13+
print( "%d is multiple of %d" %(a, b) )
14+
15+
if( a % b != 0 ):
16+
print( "%d is not multiple of %d" %(a, b) )
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
Question 2.27
3+
4+
(Integer Equivalent of a Character) Here is a peek ahead. In this chapter you learned about
5+
integers and the type int. C++ can also represent uppercase letters, lowercase letters and a considerable
6+
variety of special symbols. C++ uses small integers internally to represent each different character.
7+
The set of characters a computer uses and the corresponding integer representations for those
8+
characters are called that computer's character set. You can print a character by enclosing that character
9+
in single quotes, as with
10+
11+
cout << 'A'; // print an uppercase A
12+
13+
You can print the integer equivalent of a character using static_cast as follows:
14+
15+
cout << static_cast< int >( 'A' ); // print 'A' as an integer
16+
17+
This is called a cast operation (we formally introduce casts in Chapter 4). When the preceding
18+
statement executes, it prints the value 65 (on systems that use the ASCII character set). Write a
19+
program that prints the integer equivalent of a character typed at the keyboard. Store the input in a
20+
variable of type char. Test your program several times using uppercase letters, lowercase letters, digits
21+
and special characters (like $).
22+
'''
23+
print( ord( input( "Enter a single character: " ) ) )

0 commit comments

Comments
 (0)