Question

In: Computer Science

USING PYTHON ONLY Part 3a: Prime Number Finder Write a program that prompts the user to...

USING PYTHON ONLY

Part 3a: Prime Number Finder

Write a program that prompts the user to enter in a postive number. Only accept positive numbers - if the user supplies a negative number or zero you should re-prompt them.

Next, determine if the given number is a prime number. A prime number is a number that has no positive divisors other than 1 and itself. For example, 5 is prime because the only numbers that evenly divide into 5 are 1 and 5. 6, however, is not prime because 1, 2, 3 and 6 are all divisors of 6.

Here's a sample running of the program:

Enter a positive number to test: 5

2 is NOT a divisor of 5  ... continuing
3 is NOT a divisor of 5  ... continuing
4 is NOT a divisor of 5  ... continuing

5 is a prime number!

And here's another running:

Enter a positive number to test: 9

2 is NOT a divisor of 9  ... continuing
3 is a divisor of 9  ... stopping

9 is not a prime number.

Some notes on your program:

  • 1 is technically not a prime number.
  • Once you find a number that evenly divides into your test number you do not need to continue testing additional numbers - the number cannot be prime.

Solutions

Expert Solution

Code and output

Code for copying

n=int(input("Enter a positive number to test: "))
while n<=0:
n=int(input("Enter a positive number to test: "))
count=2
for i in range(2,n):
if n%i==0:
print("{} is a divsor of {} ....stopping".format(i,n))
print(" ")
print("{} is not a prime number.".format(n))
break
if n%i !=0:
print("{} is a NOT a divsor of {} ....continuing".format(i,n))
count+=1
if count == n:
print(" ")
print("{} is a prime number!".format(n))

Code snippet

n=int(input("Enter a positive number to test: "))
while n<=0:
    n=int(input("Enter a positive number to test: ")) 
count=2
for i in range(2,n):    
    if n%i==0:
        print("{} is a divsor of {} ....stopping".format(i,n))
        print(" ")
        print("{} is not a prime number.".format(n))
        break 
    if n%i !=0:
        print("{} is a NOT a divsor of {} ....continuing".format(i,n))
        count+=1
if count == n:
    print(" ")
    print("{} is a prime number!".format(n))

Related Solutions

(using for loop, parameters, if, else condition only )Python: Write a program that prompts the user...
(using for loop, parameters, if, else condition only )Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message...
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Write a Python program using functions and mainline logic which prompts the user to enter a...
Write a Python program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum, the numbers should...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
Write a Python program with correct indentation using functions and mainline logic which prompts the user...
Write a Python program with correct indentation using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum,...
Write a program that prompts the user to input a decimal number and outputs the number...
Write a program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer.
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user to enter the name of a field (other than Date), and then outputs the highest and lowest values recorded in that field for the month of August. The file climate_data_2017_numeric.csv contains the following fields: Date Minimum temperature (C) Maximum temperature (C) Rainfall (mm) Speed of maximum wind gust (km/h) 9am Temperature (C) 9am relative humidity (%) 3pm Temperature (C) 3pm relative humidity (%) Expected...
Part A In PyCharm, write a program that prompts the user for their name and age....
Part A In PyCharm, write a program that prompts the user for their name and age. Your program should then tell the user the year they were born. Here is a sample execution of the program with the user input in bold: What is your name? Amanda How old are you? 15 Hello Amanda! You were born in 2005. Write the program. Format your code using best practices. Refer to the zyBooks style guide, if needed, to use proper naming...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT