In: Computer Science
Please use python and run in IDLE
Question 1
Write functions that do the following:
i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters.
ii) A function that takes 2 arguments and returns the difference,
iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
Question 2
Write functions:
i) One that prompts a user for 2 numbers.
ii) Adds the two numbers if they are even
iii) Multiplies the two numbers if they are odd
iv) Displays the appropriate output to the user
You are writing 4 functions and calling them to test functionality.
PYTHON CODE
1)
def add_num(a,b):#function for addition
sum=a+b
return sum; #return value
def diff(a,b):#function for difference
diff=a-b
return diff
def call(a,b): # function for Product of the values returned by
both
e=add_num(a,b)
f=diff(a,b)
pro=e*f
print("Product of the values returned by both =",pro)
a=25 #variable declaration
b=5
call(a,b) #function call
OUTPUT
2)
def add_num(a,b):#function for addition if number is
even
sum=0
if a%2==0 and b%2==0:
sum=a+b
return sum #return value
def accept():#function to accept values
print("Enter two number : ")
a =int(input())
b = int(input())
s=add_num(a,b) #function call
p=Product(a,b) #function call
display(s,p)
def Product(a,b): # function for finding product if number is
odd
pro=0
if a%2!=0 and b%2!=0:
pro=a*b
return pro #return value
def display(s,p): #function for display
if s!=0 :
print("The sum is = ",s)
elif p!=0 :
print("The Product is = ",p)
else:
print("one numberis odd and other number is even")
accept() #function call
OUTPUT