In: Computer Science
USING PYTHON PROGRAM
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
Source COde:
def isPrime(n):
if(n<2):
return False
count=0
for i in range(2,n):
if(n%i==0):
count=count+1
break
if(count==0):
return True
else:
return False
lowest=int(input("Lowest number: "))
highest=int(input("Highest number: "))
choice=input("Would you like to identify Prime numbers in yout
table?(y/n): ")
while (not(choice=='y')):
print("Invalid command,try again")
choice=input("Would you like to identify Prime numbers in yout
table?(y/n): ")
print("+",end=" ")
for i in range(lowest,highest+1):
print(i,end=" ")
print()
print("-------------------------------------------------")
for i in range(lowest,highest+1):
print(i,"|",end=" ")
for j in range(lowest,highest+1):
if(isPrime(i+j)):
print((str(i+j)+'p'),end=" ")
else:
print(str(i+j),end=" ")
print()
Sample input and output: