Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

R4.1 Write a while loop that prints a. All squares less than n. For example, if

ID: 3689321 • Letter: R

Question

R4.1 Write a while loop that prints a. All squares less than n. For example, if n is 100, print 0 14916 25 3649 64 81. b. All positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90 c. All powers of two less than n. For example, if n is 100, print 1 2 48 16 32 64 R4.2 Write a loop that computes a. The sum of all even numbers between 2 and 100 (inclusive). b. The sum of all squares between 1 and 100 (inclusive). c. The sum of all odd numbers between a and b (inclusive). d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 +7+7=17.) R4.3 Provide trace tables for these loops. c. i 10 while i

Explanation / Answer

R4.1
a)
root = 0.0
while root * root < n:
print(root * root)
root = root + 1

b)
i = 1
while i < n:
if i%10 == 0:
print i
i = i+1

c)
i = 1
while i*2 < n :
print i*2
i= i*2

R4.2
a)
def function():
n = 100
i = 2
sum = 0
while i <= n :
if i%2 == 0:
sum = sum + i
i= i+1
print sum


b)
def function():
n = 100
i = 1
sum = 0
while i <= n :
sum = sum + i*i
i= i+1
print sum


c)
def function():
n = b
i = a
sum = 0
while i <= n :
if i%2 != 0:
sum = sum + i
i= i+1
print sum

d)
def function():
n = 32677
i = 1
sum = 0
while n != 0 :
if n%10 %2 != 0 :
sum = sum + n%10
n = n/10
print sum


R4.3
a)
i j n
0 10 0
1 9 1
2 8 3
3 7 4
4 6 5
5 5 6

b)
i j n
0 0 0
1 1 1
2 2 4
3 3 9
4 4 16
5 5 25
6 6 36
7 7 49
8 8 64
9 9 81

c)
i j n
10 0 0
9 1 10
8 2 16
7 3 20
6 4 22
5 5 22
4 6 20
3 7 16
2 8 10
1 9 2

d)
i j n
0 10 0
2 8 1
4 6 2
6 4 3
they will never be equal ,infinite loop


R4.4
a)
1
2
3
4
5
6
7
8
9

b)
1
3
5
7
9

c)
NULL

d)
0
1
2
3
4
5
6
7
8
9

e)
2
4
6
8


R4.5
A loop which dosen't terminates and keeps on getting implemented is an infinite loop,i.e it never meets the the terminating condition.
we can terminate by sending a SIGHUP (Ctrl-Z) or SIGTERM (Ctrl-C).

R.4.6
a) 10 iterations from 1 to 11.
b) 10 iterations from 0 to 9.
c) 10 iterations from 10 to 1
d) 21 iteration from -10 to 10
e) 0 iterations
f) 11 iterations
g) 7 iterations