# In-Class Exercises

Consider the following python programs below and explain what they do in detail.

In [1]:
# Exercise 1

result = 0.0
num = 0

more_input = 'yes'

while (more_input == 'yes'):

number = float(input('Enter number : '))
num += 1

result += number

more_input = input('More numbers to be entered? (yes/no) : ')

result /= num

print('Result = ',result)

Enter number : 3
More numbers to be entered? (yes/no) : yes
Enter number : 5
More numbers to be entered? (yes/no) : yes
Enter number : 1
More numbers to be entered? (yes/no) : no
Result =  3.0

In [2]:
# Exercise 2

count_1 = 0
count_2 = 0

more_input = 'yes'

while (more_input == 'yes'):

number = float(input('Enter number : '))

if number%2 == 0:
count_1 += 1
else:
count_2 += 1

more_input = input('More numbers to be entered? (yes/no) : ')

print('count_1 = ',count_1,', count_2 = ',count_2)

Enter number : 12
More numbers to be entered? (yes/no) : yes
Enter number : 11
More numbers to be entered? (yes/no) : yes
Enter number : 6
More numbers to be entered? (yes/no) : yes
Enter number : 9
More numbers to be entered? (yes/no) : no
count_1 =  2 , count_2 =  2

In [3]:
# Exercise 3

n = int(input("Please enter a positive integer larger than 2 : "))

first_number = 1
second_number = 1

print(first_number,second_number,'',end='')

for i in range(n-2):

third_number = first_number + second_number

print(third_number,'',end='')

first_number = second_number

second_number = third_number

Please enter a positive integer larger than 2 : 20
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 
In [3]:
# Exercise 4

for i in range(2,100):

flag = True

for j in range(2,i-1):
if i%j==0:
flag = False

if flag:
print(i,'',end='')

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
In [5]:
# Exercise 5

number = int(input('Enter an integer : '))

result = 1

while number != 0:

result *= number%10
number = number // 10

print('Result = ',result)

Enter an integer : 12345
Result =  120

In [6]:
# Exercise 6

org_number = int(input('Enter an integer : '))

number = org_number
another_number = 0

while number != 0:

another_number = another_number*10 + (number%10)
number = number // 10

if another_number == org_number:
print('YES!')
else:
print('NO!')

Enter an integer : 2345432
YES!

In [4]:
# Exercise 7

N = 9

for i in range(1,N+1):

for j in range(i):
print(i,end='')

print('')

1
22
333
4444
55555
666666
7777777
88888888
999999999

In [7]:
# Exercise 8

num_1 = int(input('Enter first number : '))
num_2 = int(input('Enter second number : '))

result = 1

for i in range(2,min([num_1,num_2])+1):
if (num_1%i == 0) and (num_2%i == 0):
result = i

print('Result = ',result)

Enter first number : 6
Enter second number : 18
Result =  6

In [8]:
# Exercise 9

num_1 = int(input('Enter first number : '))
num_2 = int(input('Enter second number : '))

max_num = max([num_1,num_2])
result = max_num

while (result%num_1 != 0) or (result%num_2 != 0):
result = result + max_num

print('Result = ',result)

Enter first number : 3
Enter second number : 9
Result =  9

In [9]:
# Exercise 10

number = int(input('Enter positive integer : '))

result = ''

while (number != 0):
result = str((number%2)) + result
number = number // 2

print('Result = ',result)

Enter positive integer : 12
Result =  1100

In [ ]: