In: Computer Science
USING PYTHON PROGRAM ONLY (cannot use list, max, or index function because have not learned about it in class yet)
Part C builds on parts A and B, so just need the final code from part C.
USING PYTHON PROGRAM
Part 4a: Addition Table
Write a program that prompts the user for two integers that are greater than or equal to zero. Ensure that the first integer is less than the second integer. Next, write a program that generates an "addition table" using these numbers that computes the sums of all possible values - use the output below as a guide:
Lowest number: -5 Lowest number must be 0 or greater Lowest number: 0 Highest number: 0 Highest number must be larger than lowest number! Highest number: 5 + 0 1 2 3 4 5 ---------------------------- 0 | 0 1 2 3 4 5 1 | 1 2 3 4 5 6 2 | 2 3 4 5 6 7 3 | 3 4 5 6 7 8 4 | 4 5 6 7 8 9 5 | 5 6 7 8 9 10
Here are some hints to get you started:
Lowest number: 9999 Highest number: 10004 + 9999 10000 10001 10002 10003 10004 ------------------------------------------------- 9999 | 19998 19999 20000 20001 20002 20003 10000 | 19999 20000 20001 20002 20003 20004 10001 | 20000 20001 20002 20003 20004 20005 10002 | 20001 20002 20003 20004 20005 20006 10003 | 20002 20003 20004 20005 20006 20007 10004 | 20003 20004 20005 20006 20007 20008
Lowest number: 9999999 Highest number: 10000004 + 9999999 10000000 10000001 10000002 10000003 10000004 ---------------------------------------------------------------------- 9999999 | 19999998 19999999 20000000 20000001 20000002 20000003 10000000 | 19999999 20000000 20000001 20000002 20000003 20000004 10000001 | 20000000 20000001 20000002 20000003 20000004 20000005 10000002 | 20000001 20000002 20000003 20000004 20000005 20000006 10000003 | 20000002 20000003 20000004 20000005 20000006 20000007 10000004 | 20000003 20000004 20000005 20000006 20000007 20000008
Part 4b: Addition Table
Next, add in a feature that asks the user if they want to identify 'Prime' numbers in their table. If the user elects to show prime numbers you can print a lowercase 'p' character after each prime number. Ensure that your table displays correctly, as described above.
Lowest number: 0 Highest number: 10 Would you like to identify Prime numbers in your table? (y/n): pikachu Invalid command, try again Would you like to identify Prime numbers in your table? (y/n): y + 0 1 2 3 4 5 6 7 8 9 10 ------------------------------------------------ 0 | 0 1 2p 3p 4 5p 6 7p 8 9 10 1 | 1 2p 3p 4 5p 6 7p 8 9 10 11p 2 | 2p 3p 4 5p 6 7p 8 9 10 11p 12 3 | 3p 4 5p 6 7p 8 9 10 11p 12 13p 4 | 4 5p 6 7p 8 9 10 11p 12 13p 14 5 | 5p 6 7p 8 9 10 11p 12 13p 14 15 6 | 6 7p 8 9 10 11p 12 13p 14 15 16 7 | 7p 8 9 10 11p 12 13p 14 15 16 17p 8 | 8 9 10 11p 12 13p 14 15 16 17p 18 9 | 9 10 11p 12 13p 14 15 16 17p 18 19p 10 | 10 11p 12 13p 14 15 16 17p 18 19p 20
Part 4c: Addition Table
Expand your program to support ALL of the arithmetic operators (+, -, *, /, // and %) - prompt the user for an operator to use and then display the desired table. Validate your data accordingly. Ensure that your tables print out using the expected formatting. Note that negative numbers are not considered Prime for the purpose of this part of the assignment.
Code for above program :-
def add(low,high,p):
string = '+ '
for i in range(low,high+1):
string = string + " " + str(i)
print(string)
print("-"*((high-low)*4))
for i in range(low,high+1):
string = str(i)+" "+"|"
num = i
for j in range(low,high+1):
temp = num + j
if p == 1:
if isPrime(temp):
temp = str(temp) + "p"
else:
temp = str(temp)
else:
temp = str(temp)
string = string+" "+ temp
print(string)
def isPrime(n):
if (n <= 1):
return False
if (n <= 3):
return True
if (n % 2 == 0 or n % 3 == 0):
return False
i = 5
while (i * i <= n):
if (n % i == 0 or n % (i + 2) == 0):
return False
i = i + 6
return True
def subtract(low,high,p):
string = '- '
for i in range(low,high+1):
string = string + " " + str(i)
print(string)
print("-"*((high-low)*4))
for i in range(low,high+1):
string = str(i)+" "+"|"
num = i
for j in range(low,high+1):
temp = num - j
if p == 1:
if isPrime(temp):
temp = str(temp) + "p"
else:
temp = str(temp)
else:
temp = str(temp)
string = string+" "+str(temp)
print(string)
def product(low,high,p):
string = '* '
for i in range(low,high+1):
string = string + " " + str(i)
print(string)
print("-"*((high-low)*4))
for i in range(low,high+1):
string = str(i)+" "+"|"
num = i
for j in range(low,high+1):
temp = num * j
if p == 1:
if isPrime(temp):
temp = str(temp) + "p"
else:
temp = str(temp)
else:
temp = str(temp)
string = string+" "+str(temp)
print(string)
def division(low,high,p):
string = '/ '
for i in range(low,high+1):
string = string + " " + str(i)
print(string)
print("-" * ((high - low) * 4))
string = str(0) + " " + "|"
for i in range(low,high+1):
string = string +" "+ "-"
print(string)
for i in range(low+1,high+1):
string = str(i)+" "+"|"
num = i
for j in range(low,high+1):
temp = j/num
temp = round(temp, 2)
if p == 1:
if float(temp).is_integer() and isPrime(temp):
temp = str(temp) + "p"
else:
temp = str(temp)
else:
temp = str(temp)
string = string+" "+ str(temp)
print(string)
def floor(low,high,p):
string = '// '
for i in range(low,high+1):
string = string + " " + str(i)
print(string)
print("-"*((high-low)*4))
string = str(0) + " " + "|"
for i in range(low, high + 1):
string = string + " " + "-"
print(string)
for i in range(low+1,high+1):
string = str(i)+" "+"|"
num = i
for j in range(low,high+1):
temp = j//num
if p == 1:
if isPrime(temp):
temp = str(temp) + "p"
else:
temp = str(temp)
else:
temp = str(temp)
string = string+" "+str(temp)
print(string)
def mod(low,high,p):
string = '% '
for i in range(low,high+1):
string = string + " " + str(i)
print(string)
print("-"*((high-low)*4))
string = str(0) + " " + "|"
for i in range(low, high + 1):
string = string + " " + "-"
print(string)
for i in range(low+1,high+1):
string = str(i)+" "+"|"
num = i
for j in range(low,high+1):
temp = j%num
if p == 1:
if isPrime(temp):
temp = str(temp) + "p"
else:
temp = str(temp)
else:
temp = str(temp)
string = string+" "+str(temp)
print(string)
low = int(input("Enter Lowest Number: "))
while low<0:
low = int(input("Lowest number must be 0 or greater: "))
high = int(input("Enter Highest Number: "))
while high<low:
high = int(input("Highest number must be greater than lowest number: "))
operation = input("Enter +,-,*,/,//,% to perform operation: ")
p = 0
if operation=="+":
add(low,high,p)
pr = ''
while pr.lower()!="y" or pr.lower()!='n':
pr = input("Would you like to know the prime in the above table(y/n): ")
if pr.lower() =="y":
p = 1
add(low,high,p)
break
elif pr.lower()=='n':
break
else:
print("Invalid input Try again .")
elif operation=="-":
subtract(low,high,p)
pr = ''
while pr.lower() != "y" or pr.lower() != 'n':
pr = input("Would you like to know the prime in the above table(y/n): ")
if pr.lower() == "y":
p = 1
subtract(low, high, p)
break
elif pr.lower() == 'n':
break
else:
print("Invalid input Try again .")
elif operation=="*":
product(low,high,p)
pr = ''
while pr.lower() != "y" or pr.lower() != 'n':
pr = input("Would you like to know the prime in the above table(y/n): ")
if pr.lower() == "y":
p = 1
product(low, high, p)
break
elif pr.lower() == 'n':
break
else:
print("Invalid input Try again .")
elif operation=="/":
division(low,high,p)
pr = ''
while pr.lower() != "y" or pr.lower() != 'n':
pr = input("Would you like to know the prime in the above table(y/n): ")
if pr.lower() == "y":
p = 1
division(low, high, p)
break
elif pr.lower() == 'n':
break
else:
print("Invalid input Try again .")
elif operation=="//":
floor(low,high,p)
pr = ''
while pr.lower() != "y" or pr.lower() != 'n':
pr = input("Would you like to know the prime in the above table(y/n): ")
if pr.lower() == "y":
p = 1
floor(low, high, p)
break
elif pr.lower() == 'n':
break
else:
print("Invalid input Try again .")
elif operation=="%":
mod(low,high,p)
pr = ''
while pr.lower() != "y" or pr.lower() != 'n':
pr = input("Would you like to know the prime in the above table(y/n): ")
if pr.lower() == "y":
p = 1
mod(low, high, p)
break
elif pr.lower() == 'n':
break
else:
print("Invalid input Try again .")
Screenshot for above program :-