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...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
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...
Part 2 Write a C++ program that prompts the user for an Account Number(a whole number)....
Part 2 Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a switch construct to determine the account transaction and a do…while loop to...
Part 3 Write a C++ program that prompts the user for an Account Number(a whole number)....
Part 3 Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a nested if statements instead of a Switch construct. Test Run: Enter the...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT