In: Computer Science
Write a program in PYTHON which:
*Defines the following 5 functions:
whoamI()
This function prints out your name and PRINTS OUT any programming course you have taken prior to this course.
isEven(number)
This function accepts one parameter, a number, and PRINTS OUT a message telling if the numberer is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two.
printEven(number)
This function accepts one parameter, a number, and PRINTS OUT all even values from 2 thru this number.
sumUp(number)
This function accepts one parameter, a number, and RETURNS the sum of ALL values from 1 up to this number
howmanyDigits(number)
This function accepts one parameter, a number, and RETURNS the number of digits contained in the value. (for example, if the value was 475, the function would return a 3. Hint: the simplest way to do this is to convert the number to a string .. then the length is accessible
* follows the function definition with main process statements which:
1.) calls the 'whoamI' function to that the program begins by outputting uses information
2.) prompt the user for a positive int and store it
If the user does not enter a valid number, reprompt. Continue this until a valid value
has been entered.
3.) present the user with 4 choices:
-find out if the number is even or odd
- printout all even values up to the number
-output the sum of all values from one to the number
- output the number of digits in the number
4.) Carry out the user’s choice. Be sure to call one of the functions that you have defined to do this.
If you wish you may put the main statements in a loop that repeats until the user chooses to exit.
def whoami(): #define whoami()
print("I am Robert Chalse")
print("I have covered basic languages like C,C++,JAVA")
#define isEven()
def isEven(n):
if n % 2 == 0: #condition for even numbers
print(n," is an even number")
else:
print(n,"is an odd number")
#define printEven()
def printEven(n):
for i in range(2,n+1): #loop will runs from 2 to n
if(i%2==0): #check for even
print(i,end=" ") #print the even number
#define sumUpNumber()
def sumUpNumber(n):
total=0
for i in range(1,n+1): #loop runs from 1 to n
total = total + i #find the sum of all numbers
return total #return the sum
#define howmanyDigits()
def howmanyDigits(n) :
count=0
while (n!=0): #loop will execute till number not equal to
zero
count=count+1 #count the nu,mber of digits
n = (int)(n/10)#reduce the number by one digit
return count #return count
whoami() #call whoami()
#infinite loop is used to input a positive number
#this loop will continue if user will eneter 0 or -ve numbers
while(1):
n = (int)(input("Enter a positive number"));
if (n>0): #check for positive number
break #terminate the loop
#infinite loop is used to operate the menu operations
while(1):
#display the menus
print("1. Check Even or ODD")
print("2. Print all even numbers from 1 to n")
print("3. Print the sum of all numers from 1 to n")
print("4. Count the number of digits in the number")
print("Which operation do you want : ")
opt=(int)(input("Enter your choice")) #read the choice of
user
if(opt==1):
isEven(n)
else:
if(opt==2):
printEven(n)
else:
if(opt==3):
print("")
print("Sum of Numbers from 1 to ",n," is ",sumUpNumber(n))
else:
if(opt==4):
print("The total number of digits in ",n," is
",howmanyDigits(n))
else:
print("Invalid Choice");
#ask user to ocntinue more or not
ch = input("Do you want to operate more[Y/N]")
if(ch=='N' or ch=='n'):
break; #the loop will terminate if user will eneter n or N
OUTPUT
PROGRAM IN JPEG FORMAT