####################################################################################################### ###################################################################################### ########################################################### #############################using python################### ###########################################################
# RQ3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>> largest_factor(15) # factors are 1, 3, 5 5 >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40 40 """ "*** YOUR CODE HERE ***" # RQ4 # Write functions c, t, and f such that calling the with_if_statement and # calling the with_if_function do different things. # In particular, write the functions (c,t,f) so that calling with_if_statement function returns the integer number 1, # but calling the with_if_function function throws a ZeroDivisionError. def if_function(condition, true_result, false_result): """Return true_result if condition is a true value, and false_result otherwise. >>> if_function(True, 2, 3) 2 >>> if_function(False, 2, 3) 3 >>> if_function(3==2, 3+2, 3-2) 1 >>> if_function(3>2, 3+2, 3-2) 5 """ if condition: return true_result else: return false_result
RQ3:
Code:
def largest_factor(n):
for i in range(n-1,0,-1):
#this loop executes for n-1 to 1
if(n%i==0):
#if the number is divisible by i we return the value of i
return i
print("Largest Factor of 15 is ",largest_factor(15))
print("Largest Factor of 80 is ",largest_factor(80))
#Here we call the function for various values
Output:
Indentation:
RQ4:
Code:
def if_function(condition,true_result,false_result):
if(condition):
#If true then true_result will be returned
return true_result
else:
#if false then false_result will be returned
return false_result
print(if_function(True,2,3))
print(if_function(False,2,3))
print(if_function(3==2,3+2,3-2))
print(if_function(3>2,3+2,3-2))
#Here we call the function for varius inputs
Output:
Indentation:
Get Answers For Free
Most questions answered within 1 hours.