Chapter 5: Program looping

Program 5.1, Page number: 43

In [1]:
triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;

print("The eighth triangular number is {0}".format(triangularNumber))
The eighth triangular number is 36

Program 5.2, Page number: 44

In [2]:
triangularNumber=0

for i in range (201):
   triangularNumber=triangularNumber+i   

print("The 200th triangular number is {0}".format(triangularNumber))
The 200th triangular number is 20100

Program 5.3, Page number: 47

In [3]:
print("TABLE OF TRIANGULAR NUMBERS\n\n")
print(" n          Sum from 1 to n\n")
print("---        -----------------\n")

triangularNumber=0

for i in range (1,11):
    triangularNumber=triangularNumber+i
    print(" {0}                  {1}\n".format(i,triangularNumber))
TABLE OF TRIANGULAR NUMBERS


 n          Sum from 1 to n

---        -----------------

 1                  1

 2                  3

 3                  6

 4                  10

 5                  15

 6                  21

 7                  28

 8                  36

 9                  45

 10                  55

Program 5.4, Page number: 51

In [5]:
triangularNumber=0
number=10                                #number=input("What triangular number do you want?")

for n in range (1,(number+1)):
        triangularNumber+=n

print("triangular number {0} is {1}".format(number,triangularNumber))
triangular number 10 is 55

Program 5.5, Page number: 53

In [7]:
for counter in range(1,6):                     #Outer Loop
        number=12                              #input("what triangular number do you want? ")

        triangularNumber=0                     #Variable Decaration

        for n in range (1,(number+1)):         #Inner Loop
           triangularNumber+=n
        
        #Result
        print("Triangular Number {0} is {1}".format(number,triangularNumber))
Triangular Number 12 is 78
Triangular Number 12 is 78
Triangular Number 12 is 78
Triangular Number 12 is 78
Triangular Number 12 is 78

Program 5.6, Page number: 56

In [9]:
count=1

while (count<=5):
        print("{0}\n".format(count))     #Result
        count+=1
1

2

3

4

5

Program 5.7, Page number: 58

In [11]:
print("Please type in two nonnegative integers.")
u=8                                               #u=input()
v=14                                              #v=input()

while(v!=0):
        temp=u%v
        u=v
        v=temp

print("Their greatest common divisor is {0}\n".format(u))
Please type in two nonnegative integers.
Their greatest common divisor is 2

Program 5.8, Page number: 60

In [13]:
import sys

number=113                                     #number=input("Enter your number.\n")

while(number!=0):
        right_digit=number%10
        sys.stdout.write("{0}".format(right_digit))
        number=number/10

print("\n")
311

Program 5.9, Page number: 61

In [14]:
import sys

number=428                                      #number=input("Enter your number:\n")

while True:                                     #Enter loop instantly for the first time
    right_digit=number%10
    sys.stdout.write("{0}".format(right_digit))
    number=number/10
    if(number == 0):                            #Check for exit condition
        break

print("\n")
824