SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Value Returning Functions And Modules
Exercise:
Programming Exercises
Question:8 | ISBN:9780132576376 | Edition: 2

Question

Prime Numbers
A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however,
is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and
returns True if the argument is a prime number, or False otherwise. Use the function in a program that prompts the user to enter a number and then displays a message indicating
whether the number is prime.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Prime Numbers Python code:

#The method Boolean function named is_prime 
#which takes an integer as an argument and
#returns True if the argument is a prime 
#number, or False otherwise.
def is_prime(number):
    if number < 2:
        return False
    for i in range(2, int(number**0.5) + 1):
        if number % i == 0:
            return False
    return True

# Prompt and read the input from the user
number = int(input("Enter a number: "))

# Checking if the number is prime
if is_prime(number):
    print(number, "is a prime number.")
else:
    print(number, "is not a prime number.")

Executed Output 1:

   
Enter a number: 15
15 is not a prime number.

Executed Output 2:

 
Enter a number: 11
11 is a prime number

 

0 0

Discussions

Post the discussion to improve the above solution.