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 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...
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...
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)
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:...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT