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,...
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...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
Python A program which prompts the user to enter an number which is a integer n...
Python A program which prompts the user to enter an number which is a integer n and creates a tuple where the values are all numbers between 1 and n (both inclusive). Note: you can assume that the integer will always be > 1. Input Result 3 Enter an integer: 3 (1, 2, 3)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT