Starting from:

$30

Homework 4

Homework 4

NOTE: This homework requires you to write your code in the form of functions. While the same output
can be achieved without using functions. Your code should have functions in them
Q1. 20 points
Write a function, called cubic, which takes in two parameters, say x and y and computes the following
formula: x3 + x + y and returns the computed value.
Sample output 1:
Enter x: 2
Enter y: 1
The value of the function is : 11
Sample output 2:
Enter x: 1
Enter y: 3
The value of the function is : 5
Q2. 30 points
Write a function, called factor, which takes an integer, say n, as a parameter and returns the first
number between 2 and n-1 which divides n. If no such number exists it returns -1
Sample output 1:
Enter n: 15
The smallest divisor is : 3
Sample output 2:
Enter n: 17
The smallest divisor is : -1
Sample output 3:
Enter n: 16
The smallest divisor is : 2
Q3. 50 points
A consider a password valid if it contain only alphabets and numbers. Additionally it must contain at
least one uppercase letter and at least one number and must be 7 characters or longer. Write a
function, called isValid, which takes a string as a parameter and returns the Boolean value True if it is a
valid password and the Boolean value False if it is not. Depending on what the function returns for an
inputted password output if it is valid or not.
Note: you will get the return value from the function isValid and depending on the returned value you
will print one line or the other.
Sample output 1:
Enter the password: Abc123A
The password is valid
Sample output 2:
Enter the password: hello6
The password is not valid
Sample output 3:
Enter the password: xy_abc
The password is not valid

More products